开发者

Multiple Swipe views

开发者 https://www.devze.com 2023-03-21 04:59 出处:网络
So I\'m trying to create an app that changes a label based on the direction you swipe. However, instead of being able to swipe anywhere on the screen I\'d like it to only swipe on a specific view. I p

So I'm trying to create an app that changes a label based on the direction you swipe. However, instead of being able to swipe anywhere on the screen I'd like it to only swipe on a specific view. I put a new view on the .xib file and used that view for the location of touches and named it leftView, but it still uses the whole screen. I'm using a touchesBegan and touchesMoved method so I can use diagonals. Here's a bit of the code I'm using:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:leftView];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:leftView];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if ((deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) && currentPosition.x < gestureStartPoint.x){
    label.text = @"Left Horizontal swipe detected";
}
else if((deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) && currentPosition.x > gestureStartPoi开发者_运维技巧nt.x){
    label.text = @"Right Horizontal swipe detected";
}
else if ((deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) && currentPosition.y < gestureStartPoint.y){
    label.text = @"Up Vertical swipe detected";
}
else if ((deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) && currentPosition.y > gestureStartPoint.y){
    label.text = @"Down Vertical swipe detected";
}


You can achieve what you're after by adding two UIGestureRecognizers (one for right swipe and another for left swipe) to the UIView that you want to receive the swipes.

Apple's SimpleGestureRecognizer sample project gives a great illustration of how to set this up.


Check the identity inspector whether the uiview class is selected for the UIView object in organizer. I guess u have implemented the touchesBegan method in the sub class of UIView.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号