手势

来源:互联网 发布:java http获取cookie 编辑:程序博客网 时间:2024/05/01 19:51
文章只要你有一点点基础应该就可以看的懂,文章只为学习交流#import "ViewController.h"@interface ViewController ()@property (nonatomic,retain)UIImageView *imageView;@property (nonatomic,assign)NSInteger index;//下标@property (nonatomic,retain)NSMutableArray *images;//图片名    字数组@end@implementation ViewController#加载视图-(void)viewDidLoad{[super viewDidLoad];//布局imageView[self layoutImageView];//1.创建轻拍手势[self tapGestureRecognizer];//2.创建清扫手势[self swipeGestureRecognizer];//3.创建长安手势[self longPressGestureRecognizer];//4.创建平移手势[self panGestureTecognizer];//5.创建捏合手势[self pinchGestureRecognizer];//6.创建 旋转手势[self rotationGestureRecognizer]//7.创建边缘手势[self screenEdgePanGestureRecognizer];}##布局ImageView-(void)layoutImageView{//创建对象UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];//配置属性imageView.backgroundColor =[UIColor purpleColor];//设置图片imageView.image =[UIImage imageNamed:@"car1.jpg"];//添加父视图[self.view addSubview:imageView];//将创建的图片视图对象 给属性赋值self.imageView =imageView;//打开用户交互self.imageView.userInteractionEnabled =YES;self.images =[NSMutableArray array];for (int i = 1; i<9; i++) {    NSString * imageName =[NSString stringWithFormat:@"car%d.jpg",i];    [_images addObject:imageName];}// _index =1;}#pragma 轻怕手势//创建轻拍手势-(void)tapGestureRecognizer{//创建手势对象UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self     action:@selector(tapAction:)];//配置属性//轻拍次数tap.numberOfTapsRequired =1;//轻拍手指个数tap.numberOfTouchesRequired =1;//讲手势添加到指定的视图上[_imageView addGestureRecognizer:tap];}//轻拍事件-(void)tapAction:(UITapGestureRecognizer *)tap{//图片切换NSLog(@"拍一下");_index ++;if (_index == 8) {    _index = 1;}self.imageView.image =[UIImage imageNamed:_images[_index]];}#pragma 清扫手势//清扫手势-(void)swipeGestureRecognizer{UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];//配置属性//一个清扫手势  只能有两个方向(上和下) 或者 (左或右)//如果想支持上下左右清扫  那么一个手势不能实现  需要创建两个手势swipe.direction =UISwipeGestureRecognizerDirectionLeft;//添加到指定视图[_imageView addGestureRecognizer:swipe];UISwipeGestureRecognizer *swipe2 =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];swipe2.direction =UISwipeGestureRecognizerDirectionRight;[_imageView addGestureRecognizer:swipe2];}//清扫事件-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{NSLog(@"扫一下");if (swipe.direction ==UISwipeGestureRecognizerDirectionRight)       {    NSLog(@"右清扫");    _index --;    if (_index < 1) {        _index =7;    }    _imageView.image =[UIImage imageNamed:_images[_index]];}else if(swipe.direction == UISwipeGestureRecognizerDirectionLeft){    NSLog(@"左扫一下");    _index ++;    if (_index == 8) {        _index=1;    }    _imageView.image =[UIImage imageNamed:_images[_index]];}}#pragma 长按手势//创建长按手势-(void)longPressGestureRecognizer{UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];//最短长按时间longPress.minimumPressDuration =2;//允许移动最大距离longPress.allowableMovement =1;//添加到指定视图[_imageView addGestureRecognizer:longPress];}//长按事件-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{//NSLog(@"长按");//对于长安有开始和 结束状态if (longPress.state == UIGestureRecognizerStateBegan) {    NSLog(@"长按开始");    //将图片保存到相册    UIImage *image =[UIImage imageNamed:_images[_index]];    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);}else if (longPress.state == UIGestureRecognizerStateEnded){    NSLog(@"长按结束");}}#pragma 平移手势//创建平移手势-(void)panGestureTecognizer{UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];//添加到指定视图[_imageView addGestureRecognizer:pan];}//创建平移事件-(void)panAction:(UIPanGestureRecognizer *)pan{//获取手势的位置CGPoint position =[pan translationInView:_imageView];//通过stransform 进行平移交换_imageView.transform = CGAffineTransformTranslate(_imageView.transform, position.x, position.y);//将增量置为零[pan setTranslation:CGPointZero inView:_imageView];}#pragma 捏合手势-(void)pinchGestureRecognizer{UIPinchGestureRecognizer *pinch =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];//添加到指定视图[_imageView addGestureRecognizer:pinch];}//添加捏合事件-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{//通过 transform(改变) 进行视图的视图的捏合_imageView.transform =CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);//设置比例 为 1pinch.scale = 1;}#pragma 旋转手势//创建旋转手势-(void)rotationGestureRecognizer{UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];//添加到指定的视图[_imageView addGestureRecognizer:rotation];}//旋转事件-(void)rotationAction:(UIRotationGestureRecognizer *)rote{//通过transform 进行旋转变换_imageView.transform = CGAffineTransformRotate(_imageView.transform, rote.rotation);//将旋转角度 置为 0rote.rotation = 0;}#pragma 边缘手势//创建边缘手势-(void)screenEdgePanGestureRecognizer{UIScreenEdgePanGestureRecognizer *screenPan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPanAction:)];[_imageView addGestureRecognizer:screenPan];}//创建边缘事件-(void)screenPanAction:(UIScreenEdgePanGestureRecognizer *)screenPan{NSLog(@"边缘");}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.}@end作者:MangoJ链接:http://www.jianshu.com/p/207ca4043c93來源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原创粉丝点击