本文主要是介绍8.4 Detecting Long Press Gestures,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
长按
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressGestureRecognizer;
@property (nonatomic, strong) UIButton *dummyButton;
@end
@implementation ViewController
- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
/* Here we want to find the midpoint of the two fingers
that caused the long press gesture to be recognized. We configured this number using the numberOfTouchesRequired property of the UILongPressGestureRecognizer that we instantiated in the viewDidLoad instance method of this View Controller. If we
find that another long press gesture recognizer is using this method as its target, we will ignore it */
if ([paramSender isEqual:self.longPressGestureRecognizer]){
if (paramSender.numberOfTouchesRequired == 2){
CGPoint touchPoint1 = [paramSender locationOfTouch:0 inView:paramSender.view];
CGPoint touchPoint2 = [paramSender locationOfTouch:1 inView:paramSender.view];
CGFloat midPointX = (touchPoint1.x + touchPoint2.x) / 2.0f;
CGFloat midPointY = (touchPoint1.y + touchPoint2.y) / 2.0f;
CGPoint midPoint = CGPointMake(midPointX, midPointY);
self.dummyButton.center = midPoint;
} else {
/* This is a long press gesture recognizer with more
or less than 2 fingers */
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.dummyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.dummyButton.frame = CGRectMake(0.0f,0.0f, 72.0f, 37.0f);
self.dummyButton.center = self.view.center;
[self.view addSubview:self.dummyButton];
/* First create the gesture recognizer */
self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPressGestures:)];
/* The number of fingers that must be present on the screen */
self.longPressGestureRecognizer.numberOfTouchesRequired = 2;
/* Maximum 100 points of movement allowed before the gesture is recognized */
self.longPressGestureRecognizer.allowableMovement = 100.0f;
/* The user must press two fingers (numberOfTouchesRequired) for at least one second for the gesture to be recognized */
self.longPressGestureRecognizer.minimumPressDuration = 1.0;
/* Add this gesture recognizer to the view */
[self.view addGestureRecognizer:self.longPressGestureRecognizer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
实现的效果是:长按屏幕上的两点,1秒钟后,按钮自动跳到这两点的中间,这时可进行移动
这篇关于8.4 Detecting Long Press Gestures的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!