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;
}
[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;
}
相关文章推荐
- iOS使用xib自定义UIView
- DP+BIT(优化复杂度) UESTC 1217 The Battle of Chibi
- UUID of GATT descriptor
- 苹果开发 笔记(85)UIActionSheet 和UIAlertView 已经失效
- 解决Android Studio卡在building *** gradle project info
- UILabel、UITextField、UIButton
- UUID of GATT characteristic
- DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
- UIScrollView 常用知识点
- 做手机 UI 如何根据手机分辨率在 PS 建画布?
- [LeetCode] Binary Tree Longest Consecutive Sequence
- UUID of GATT service
- UIScrollView
- 亚博 Arduino智能小车实验报告
- java String StringBuilder StringBuffer 以及各种赋值和注意
- MIT算法导论——第四讲.Quicksort
- MIT算法导论——第三讲.The Divide-and-Conquer
- UISwitch简单用法和监听动画(类似QQ电话)实时显示
- JSP EL表达式 param、paramValues的使用
- [置顶] 【android】音乐播放器之UI设计的点点滴滴