手势
来源:互联网 发布:广电网络是上市公司吗 编辑:程序博客网 时间:2024/04/26 20:45
手势
点击(长按)、拖拽、旋转、捏合、轻扫都属于手势,继承自UIGestureRecognizer这个父类
一、添加背景颜色
self.view.backgroundColor = [UIColor whiteColor];
注:如果不设置背景颜色的话,有可能不会响应手势
手势如果不响应:
1)看是否添加到了imageView上,如果是,把userInteractionEnabled设置成YES
2)查看背景颜色是否设置,如果没有设置,就设置背景颜色
3)手势冲突,截获触发事件、手势,设置优先级
二、点击(UITapGestureRecognizer)
1)初始化
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
2)设置点击手指的个数
tap.numberOfTouchesRequired = 1;
3)设置手指点击的次数
tap.numberOfTapsRequired = 1;
4)添加到View
[self.view addGestureRecognizer:tap];
注:
tapAction:为点击手势触发时执行的事件
具体实现:
- (void)tapAction:(UITapGestureRecognizer *)sender {
// 恢复视图的初始形态(transform和手势没有关系)
imageView.transform = CGAffineTransformIdentity;
// 得到点击处的坐标值
CGPoint tapLoc = [sender locationInView:self.view];
// 分别输出x、y的坐标,用于检测与验证
NSLog(@"%f %f", tapLoc.x, tapLoc.y);
// 实现一个动画的效果
[UIView animateWithDuration:1 animations:^{
// 点击的位置设置为图片的中心点
imageView.center = tapLoc;
}];
三、长按(UILongPressGestureRecognizer)
1)初始化
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
2)最小按下的持续时间(默认0.5秒)
longPress.minimumPressDuration = 1;
3)设置手指点击的次数
longPress.numberOfTouchesRequired = 2;
4)把手势添加到某个视图上
[self.view addGestureRecognizer:longPress];
注:
longPress:为长按手势触发时执行的事件
具体实现
- (void)longPress:(UILongPressGestureRecognizer *)sender {
CGPoint tapLoc = [sender locationInView:self.view];
[UIView animateWithDuration:2 animations:^{
imageView.center = tapLoc;
}];
}
四、拖拽(UIPanGestureRecognizer)
1)初始化
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
1)设置最少的手指个数
pan.minimumNumberOfTouches = 1;
2)设置最多的手指个数
pan.maximumNumberOfTouches = 2;
3)添加手势
[self.view addGestureRecognizer:pan];
注:
pan:为拖拽手势触发时执行的事件
具体实现
- (void)pan:(UIPanGestureRecognizer *)sender {
CGPoint tapLoc = [sender locationInView:self.view];
imageView.center = tapLoc;
}
五、轻扫(UISwipeGestureRecognizer)
1)初始化
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
2)设置轻扫方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
3)处理拖拽和轻扫两个手势的冲突(需要轻扫手势执行完毕之后再去执行拖拽手势)
[pan requireGestureRecognizerToFail:swipe];
4、添加手势到某个视图
[self.view addGestureRecognizer:swipe];
注:
swipe:为轻扫手势触发时执行的事件
具体实现
- (void)swipe:(UISwipeGestureRecognizer *)sender {
self.view.frame = CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds), 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
[UIView animateWithDuration:2 animations:^{
self.view.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
}];
}
六、捏合(UIPinchGestureRecognizer)
1)初始化
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
2)添加到某个视图
[self.view addGestureRecognizer:pinch];
注:
pinch:为捏合手势触发时执行的事件
具体实现
- (void)pinch:(UIPinchGestureRecognizer *)sender {
// 通过手势得到变化比例,让imageView的形态发生改变
imageView.transform = CGAffineTransformScale(imageView.transform, sender.scale, sender.scale);
imageView.center = self.view.center;
}
七、旋转(UIRotationGestureRecognizer)
1)初始化
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
2)添加到某个视图
[self.view addGestureRecognizer:rotation];
注:
rotation:为旋转手势触发时执行的事件
具体实现
- (void)rotation:(UIRotationGestureRecognizer *)sender {
// 通过旋转手势让图片进行相同的变形
imageView.transform = CGAffineTransformMakeRotation(sender.rotation);
}
例:找色块游戏的简单实现
#import "ViewController.h"
#define WIDTH CGRectGetWidth([UIScreen mainScreen].bounds) //屏幕的宽
#define HIGHT CGRectGetHeight([UIScreen mainScreen].bounds) //屏幕的高
@interface ViewController ()
{
UIImageView *backgroundImage;
NSArray *colorList; //颜色数组
UIView *view; //色块
int n; //循环次数
int numToButtun; //确定随机色块
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
n = 2; //设置初始循环量
colorList = @[[UIColor darkGrayColor], [UIColor lightGrayColor], [UIColor whiteColor], [UIColor grayColor], [UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor yellowColor], [UIColor magentaColor], [UIColor orangeColor], [UIColor purpleColor], [UIColor brownColor]];
[self creatView:n];
}
// 创建色块
- (void)creatView:(int)num {
// 添加背景图
backgroundImage = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
backgroundImage.image = [UIImage imageNamed:@"bg"];
[self.view addSubview:backgroundImage];
// num:循环次数 number:颜色数组的随机下标值
int number = arc4random()%colorList.count;
numToButtun = arc4random()%(num*num);
for (int i = 0; i < num; i ++) {
for (int j = 0; j < num; j ++) {
view = [[UIView alloc]initWithFrame:CGRectMake(10 + ((WIDTH - ((num+1)*10))/num + 10) * j, 100 + ((WIDTH - ((num+1)*10))/num + 10) * i , (WIDTH - ((num+1)*10))/num, (WIDTH - ((num+1)*10))/num)];
view.tag = 1+(num*j+i);
view.backgroundColor = colorList[number];
if (view.tag == (numToButtun + 1)) {
view.alpha = 0.7;
// 点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(judge)];
[view addGestureRecognizer:tap];
}
[self.view addSubview:view];
}
}
}
- (void)judge {
n += 1;
if (n < 21) {
[view removeFromSuperview];
[self creatView:n];
}else{
n = 20;
[view removeFromSuperview];
[self creatView:n];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- 手势
- hdu 3172 Virtual Friends(map)
- C++中的类型擦除(type erasure in c++)
- Caffe Windows版本的编译
- jboss配置多个实例
- websocket
- 手势
- IOS定时器
- 神经网络-第一章
- centos浅谈之rpm和rpmbuild
- eclipse无法查看源代码(包括导入源码和导入后无法查看的情况)
- 白:《数据在混合存储系统中的放置 》总结
- 【BZOJ】3155 Preprefix Sum
- java:String类的一些方法
- 解析MX记录