您的位置:首页 > 产品设计 > UI/UE

ui中各种手势的用法

2015-10-30 17:11 387 查看
- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor
whiteColor];

UIImageView *image = [[UIImageView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
image.backgroundColor = [UIColor
blackColor];
image.image = [UIImage
imageNamed:@"4444.jpg"];

image.contentMode =
UIViewContentModeScaleAspectFit;
[self.view
addSubview:image];
[image
release];

// 1. 点击(tap)

UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(tapClick:)];

// 执行方法需要点击的次数

tap.numberOfTapsRequired =
2;

// 执行方法需要的触点

tap.numberOfTouchesRequired =
1;

[image addGestureRecognizer:tap];

image.userInteractionEnabled =
YES;
[tap
release];

// 2.长按

UILongPressGestureRecognizer *l = [[UILongPressGestureRecognizer
alloc]initWithTarget:self
action:@selector(longp:)];

// 默认 0。5 --长按的时间

l.minimumPressDuration =
3;

//长按可以移动的距离

l.allowableMovement =
100;

[image addGestureRecognizer:l];
[l
release];

// 3.旋转

UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc]
initWithTarget:self
action:@selector(rotation:)];

//
rotation.rotation =
111;

//

[image
addGestureRecognizer:rotation];
[rotation
release];

// 4.捏合

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer
alloc]initWithTarget:self
action:@selector(pinch:)];

[image addGestureRecognizer:pinch];
[pinch
release];

// 5.平移

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(pan:)];

//

[image addGestureRecognizer:pan];
[pan
release];

// 6.轻扫手势

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc]
initWithTarget:self
action:@selector(swipe:)];

[image addGestureRecognizer:swipe];
[swipe
release];

// 7.屏幕边缘手势

UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer
alloc]initWithTarget:self
action:@selector(screen:)];

[image
addGestureRecognizer:screen];
[screen
release];
}

- (void)screen:(UIScreenEdgePanGestureRecognizer *)se {

NSLog(@"屏幕边缘");
}

- (void)swipe:(UISwipeGestureRecognizer *)s {

NSLog(@"轻扫");
}

- (void)pan:(UIPanGestureRecognizer *)p {

NSLog(@"平移");

// 获取视图

UIView *imageV = p.view;

CGPoint point = [p
translationInView:imageV];
imageV.transform =
CGAffineTransformTranslate(imageV.transform, point.x, point.y);

[p setTranslation:CGPointZero
inView:imageV];

}

- (void)pinch:(UIPinchGestureRecognizer *)p {

NSLog(@"捏合");

// 很重要的一个属性scale,会被捕获到缩放的倍数

// 参数一:原来的transform

// 参数二:水平方向缩放的倍数

// 参数三:垂直方向缩放的倍数

// CGAffineTansform 仿射变换矩阵(矩阵中放置view的缩放倍数,旋转角度,x,y坐标等参数)

// 获取手势所在的view

UIView *imageV = p.view;

// 将x,y方向的缩放倍数传给transform

p.view.transform =
CGAffineTransformScale(p.view.transform, p.scale, p.scale);

// 会被多次调用这个方法,所以每次都要重置缩放倍数为原始倍数
p.scale =
1.0f;
}

- (void)rotation:(UIRotationGestureRecognizer *)r {

NSLog(@"旋转");

// 改变矩阵的角度参数

r.view.transform =
CGAffineTransformRotate(r.view.transform, r.rotation);

// 重置角度,因为在这个方法在旋转时会时时被调用
r.rotation =
0.0f;
}

- (void)longp:(UILongPressGestureRecognizer *)l {

NSLog(@"长按");
}

- (void)tapClick:(UITapGestureRecognizer *)tap {

NSLog(@"点击手势");
}

// 设置同时可以有两个手势同时识别

// 基本上是同时识别两个,默认为NO
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

return
YES;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: