gpt4 book ai didi

iphone - 如何使用 UIGestureRecognizer 子类实现滑动

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:52:37 24 4
gpt4 key购买 nike

如何使用 UIGestureRecognizer 子类实现滑动?

(如果您想知道我为什么要这样做而不是使用 UISwipeGestureRecognizer,那是因为我想将滑动识别添加到 Chameleon 的 UIKit 端口)

最佳答案

我的第一次尝试(也在 Github 上):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
_beganLocation = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint movedLocation = [touch locationInView:self.view];
CGFloat distance = _distance(_beganLocation, movedLocation);

if (distance < SWIPE_MIN_DISTANCE) return;

CGFloat angle = _angle(_beganLocation, movedLocation);
int direction = -1;
if (angle > 270 - SWIPE_MAX_ANGLE && angle < 270 + SWIPE_MAX_ANGLE) {
direction = UISwipeGestureRecognizerDirectionUp;
}
if (angle > 180 - SWIPE_MAX_ANGLE && angle < 180 + SWIPE_MAX_ANGLE) {
direction = UISwipeGestureRecognizerDirectionLeft;
}
if (angle > 90 - SWIPE_MAX_ANGLE && angle < 90 + SWIPE_MAX_ANGLE) {
direction = UISwipeGestureRecognizerDirectionDown;
}
if ((angle > 360 - SWIPE_MAX_ANGLE && angle <= 360) || (angle >= 0 && angle <= SWIPE_MAX_ANGLE)) {
direction = UISwipeGestureRecognizerDirectionRight;
}

if (direction == -1) {
self.state = UIGestureRecognizerStateFailed;
} else {
self.state = self.direction == direction ? UIGestureRecognizerStateRecognized : UIGestureRecognizerStateFailed;
}
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.state = UIGestureRecognizerStateFailed;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
self.state = UIGestureRecognizerStateFailed;
}

辅助功能:

static CGFloat _distance(CGPoint point1,CGPoint point2)
{
CGFloat dx = point2.x - point1.x;
CGFloat dy = point2.y - point1.y;
return sqrt(dx*dx + dy*dy);
};

static CGFloat _angle(CGPoint start, CGPoint end)
{
CGPoint origin = CGPointMake(end.x - start.x, end.y - start.y); // get origin point to origin by subtracting end from start
CGFloat radians = atan2f(origin.y, origin.x); // get bearing in radians
CGFloat degrees = radians * (180.0 / M_PI); // convert to degrees
degrees = (degrees > 0.0 ? degrees : (360.0 + degrees)); // correct discontinuity
return degrees;
}

需要注意的是Chameleon手势识别器的实现不完整并且包含很可能特定于 Twitterrific 的逻辑。 Our fork包含进一步的修改。

关于iphone - 如何使用 UIGestureRecognizer 子类实现滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9954830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com