IOS开发(71)之长按手势
1 前言
UILongPressGestureRecognizer 用来监听并捕获到用户用手指长久按住屏幕的某一个地方的手势事件。
2 代码实例
ZYViewController.m
[plain]
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.dummyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.dummyButton.frame = CGRectMake(0.0f,0.0f, 100.0f, 37.0f);
self.dummyButton.center = self.view.center;
[self.dummyButton setTitle:@"注意我的位置" forState:UIControlStateNormal];
[self.view addSubview:self.dummyButton];
/*第一次创建手势识别器*/
self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPressGestures:)];
/* numberOfTouchesRequired这个属性保存了有多少个手指点击了屏幕,因此你要确保你每次的点击手指数目是一样的,默认值是为 0. */
self.longPressGestureRecognizer.numberOfTouchesRequired = 2;
/* Maximum 100 pixels of movement allowed before the gesture is recognized */
/*最大100像素的运动是手势识别所允许的*/
self.longPressGestureRecognizer.allowableMovement = 100.0f;
/*这个参数表示,两次点击之间间隔的时间长度。*/
self.longPressGestureRecognizer.minimumPressDuration = 1.0;
[self.view addGestureRecognizer:self.longPressGestureRecognizer];
}
- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
if ([paramSender isEqual:self.longPressGestureRecognizer]){
//如果按下手指数为2
if (paramSender.numberOfTouchesRequired == 2){
/********获得两个手指间的中点 Start********/
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);
/********获得两个手指间的中点 End********/
self.dummyButton.center = midPoint;
}
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.dummyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.dummyButton.frame = CGRectMake(0.0f,0.0f, 100.0f, 37.0f);
self.dummyButton.center = self.view.center;
[self.dummyButton setTitle:@"注意我的位置" forState:UIControlStateNormal];
[self.view addSubview:self.dummyButton];
/*第一次创建手势识别器*/
self.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPressGestures:)];
/* numberOfTouchesRequired这个属性保存了有多少个手指点击了屏幕,因此你要确保你每次的点击手指数目是一样的,默认值是为 0. */
self.longPressGestureRecognizer.numberOfTouchesRequired = 2;
/* Maximum 100 pixels of movement allowed before the gesture is recognized */
/*最大100像素的运动是手势识别所允许的*/
self.longPressGestureRecognizer.allowableMovement = 100.0f;
/*这个参数表示,两次点击之间间隔的时间长度。*/
self.longPressGestureRecognizer.minimumPressDuration = 1.0;
[self.view addGestureRecognizer:self.longPressGestureRecognizer];
}
- (void) handleLongPressGestures:(UILongPressGestureRecognizer *)paramSender{
if ([paramSender isEqual:self.longPressGestureRecognizer]){
//如果按下手指数为2
if (paramSender.numberOfTouchesRequired == 2){
/********获得两个手指间的中点 Start********/
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);
/********获得两个手指间的中点 End********/
self.dummyButton.center = midPoint;
}
}
}
运行结果
两个手指长按后的结果(也可以使用Option+鼠标长按)
补充:移动开发 , IOS ,