您的位置:首页 > 移动开发 > IOS开发

iOS 六种手势

2015-10-29 09:04 525 查看
1.点击手势

-(void)testTap{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealTap:)];
[_imageView
addGestureRecognizer:tap];
}
-(void)dealTap:(UITapGestureRecognizer *)tap{
CGPoint point = [tap
locationInView:_imageView];
NSLog(@"x=%f y=%f",point.x,point.y);
}

2.拖动手势

-(void)testPan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealPan:)];
[_imageView
addGestureRecognizer:pan];
}

-(void)dealPan:(UIPanGestureRecognizer *)pan{
if (pan.state ==
UIGestureRecognizerStateBegan) {
_startPoint =
_imageView.center;
}

// locationInView和translationInView的区别
// 1 translationInView是UIPanGestureRecognizer下面的一个属性
// locationInView则是UIGestureRecognizer下面的属性
// 2 translationInView 在指定的坐标系中移动
// locationInView 通常是指单点位置的手势 得到当前点击下在指定视图中位置的坐标
if (pan.state ==
UIGestureRecognizerStateChanged) {
CGPoint offset = [pan
translationInView:self.view];
_imageView.center =
CGPointMake(_startPoint.x+offset.x,
_startPoint.y+offset.y);
}
}

3.旋转手势
-(void)testRotation{

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealRotation:)];
[_imageView
addGestureRecognizer:rotation];
}
-(void)dealRotation:(UIRotationGestureRecognizer *)rotation{
double r = rotation.rotation;
_imageView.transform =
CGAffineTransformMakeRotation(r);
}

4.缩放手势

-(void)testPinch{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealPinch:)];
[_imageView
addGestureRecognizer:pinch];
}
-(void)dealPinch:(UIPinchGestureRecognizer *)pinch{
double scale = pinch.scale;
_imageView.transform =
CGAffineTransformMakeScale(scale, scale);
}

5.滑动手势

-(void)testSwipe{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealSwipe:)];
swipe.direction =
UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
[_imageView
addGestureRecognizer:swipe];
}
-(void)dealSwipe:(UISwipeGestureRecognizer *)swipe{
_index ++;
if (_index ==
_images.count) {
_index =
0;
}
_imageView.image = [UIImage
imageNamed:_images[_index]];
}

6.长按手势

-(void)testLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealLongPress:)];
[_imageView
addGestureRecognizer:longPress];
}
-(void)dealLongPress:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"longPress");//这句话会被打印两边
//注意:
这个事件处理方法会执行两次
if (longPress.state ==
UIGestureRecognizerStateBegan)
{
UIAlertView *alertView = [[UIAlertView
alloc] init];
alertView.message =
@"真的要保存吗";
[alertView addButtonWithTitle:@"确定"];
[alertView show];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: