iOS开发之手势篇
来源:互联网 发布:内向者适合做淘宝吗 编辑:程序博客网 时间:2024/06/06 01:19
手势就是有规律的触摸.
UIGestureRecognizer 代表iOS中的手势类,是具体手势类的父类.
共有6个手势类分别是:1.点击 2.长按 3.轻扫 4.旋转 5.捏合 6.拖拽
1). UITapGestureRecognizer (点击手势)
创建一个点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfacti on:@selector(changeBrackgroud:)];
// 触发谁的(self指当前类的对象) 什么方法(改变背景颜色的方法)
[self.viewaddGestureRecognizer:tap];
// 向视图添加tap 这个点击手势(当前tap的引用计数为2)
[taprelease];
// (引用计数减一)
- (void)changeBrackground:(UIGestureRecognizer *)tap
// tap 触发的方法(改变背景颜色) 形参的类型是UIGestureRecognizer
{
UIView *aView = tap.view;
// tap.view 获取当前点击手势所在的视图
aView.backgroundColor = [UIColorcolorWithRed:arc4random() %256 / 255.0 green:arc4random() %256 /255.0blue:arc4random() %256 /255.0alpha:1.0];
//其中用到了三基色调色的方法,其中每个值的取值范围都是0 ~ 1,而基色的取值范围是0 ~ 255, 所以要除以255.0, (alpha表示透明度 0 ~ 1)
}
UITapGestureRecognizer 其中的属性
numberOfTapsRequired点击几次才会触发事件 (默认值是1)
tap.numberOfTapsRequired =2;//点击2次触发
numberOfTouchesRequired需要几个手指点击 (默认值是1)
tap.numberOfTouchesRequired =2;//需要两个手指点击
2).UILongPressGestureRecognizer
// 创建长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(changeBrackground:)];
//为视图添加长按手势 (默认响应时间是0.5s)
// 设置响应时间
longPress.minimumPressDuration =2;//两秒之后才触发
[self.viewaddGestureRecognizer:longPress];
[longPressrelease];
UIGestureRecognizerState//状态
UIGestureRecognizerStateCancelled//取消
UIGestureRecognizerStateCancelled//失败
UIGestureRecognizerStateBegan//开始
UIGestureRecognizerStateChanged//改变
例:
//在手势开始的时候改变背景颜色
if (tap.state == UIGestureRecognizerStateBegan) {
aView.backgroundColor = [UIColor colorWithRed:arc4random() %256 /255.0 green:arc4random() %256 /255.0 blue:arc4random() %256 /255.0 alpha:1.0];
}
3). UISwipeGestureRecognizer (轻扫手势)
direction// 轻扫的方向 (默认从左向右轻扫, 每个手势只能加一个方向)
UISwipeGestureRecognizerDirectionRight // 向右
UISwipeGestureRecognizerDirectionLeft // 向左
UISwipeGestureRecognizerDirectionUp // 向上
UISwipeGestureRecognizerDirectionDown // 向下
// 创建清扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeBrackground:)];
swipe.direction = UISwipeGestureRecognizerDirectionDown;// 向下轻扫
// 添加清扫手势
[self.viewaddGestureRecognizer:swipe];
[swiperelease];
4). UIPanGestureRecognizer (拖拽手势)
// 创建拖拽手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];
// 添加手势
[viewaddGestureRecognizer:pan];
[panrelease];
// 手势触发的方法
- (void)panAction:(UIPanGestureRecognizer *)pan
{
//基于手指的起始点的偏移量
CGPoint point = [pantranslationInView:pan.view];
//当前视图偏移量跟随手指移动而一块移动
pan.view.transform =CGAffineTransformMakeTranslation(point.x, point.y);
}
5).UIPinchGestureRecognizer (捏合手势)
// 创建捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];
// 添加手势
[viewaddGestureRecognizer:pinch];
[pinchrelease];
- (void)pinch:(UIPinchGestureRecognizer *)pinch
{
pinch.scale//缩放比例
//将视图进行等比例放大
pinch.view.transform =CGAffineTransformMakeScale(pinch.scale, pinch.scale);
}
6).UIRotationGestureRecognizer (旋转手势)
// 创建旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationAction:)];
// 添加旋转手势
[viewaddGestureRecognizer:rotation];
[rotationrelease];
}
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
// 让view旋转 旋转角度 rotation 是旋转手势里的旋转角度属性;
rotation.view.transform =CGAffineTransformMakeRotation(rotation.rotation);
}
- iOS开发之手势篇
- IOS开发之手势
- iOS开发之 手势
- iOS 开发之手势
- IOS开发之手势(UIGestureRecognizer)
- IOS开发之手势UIGestureRecognizer
- iOS开发之手势(UIGestureRecognizer)
- iOS开发之手势识别
- IOS开发之手势UIGestureRecognizer
- iOS开发之手势识别
- iOS开发之手势识别
- iOS开发之手势识别
- iOS开发之自定义手势
- iOS开发之手势识别
- iOS开发之UI手势
- iOS开发之手势识别
- iOS开发之手势识别
- IOS开发之手势——UIGestureRecognizer 手势共存
- 分析EOF究竟是什么
- 邮件标签
- 一个横幅广告
- 校企结合培养创新型生物制药高技能人才
- mkinitramfs脚本文件分析,位置/usr/sbin/mkinitramfs
- iOS开发之手势篇
- 设计模式心得:策略模式 (strategy pattern)
- 在Ubuntu上安裝Eclipse
- poj 1860 最短路
- 1.25
- 一个关于家具的横幅广告
- 抓包神器fiddler
- UIScrollView的重要属性总结,以及UIScrollViewDelegate协议的使用
- 《时间管理》读后感