手势

作者: 庄栋栋 | 来源:发表于2017-03-27 00:00 被阅读0次
#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);
//设置比例 为 1
pinch.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);
//将旋转角度 置为 0
rote.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

相关文章

  • 手势

    点击手势 捏合手势 旋转手势 轻扫手势 拖拽手势 边缘平移手势 长按手势

  • iOS-手势详细参数说明

    敲击手势 长按手势 滑动手势 拖动手势 旋转手势 捏合手势 两种手势作用在同一个视图

  • 【iOS学习】——手势识别

    iOS 手势 手势需要开启用户交互 点击手势 单击手势 双击手势 添加 numberOfTapsRequired...

  • Swift - UIGestureRecognizer 各种手势

    1、点击手势2、拖动手势3、长按手势4、滑动手势5、捏合手势6、旋转手势 完整代码

  • iOS七种手势详解

    1、轻拍手势 2、捏合手势 3、旋转手势 4、平移手势 5、边缘轻扫手势 6、长按手势 7、轻扫手势 给image...

  • iOS手势总结

    1.轻拍手势 2.长按手势 3.轻扫手势 4.平移手势 5.捏合手势 6.旋转手势 7.边缘手势

  • iOS 手势

    修改时间: 2016-12-19修改次数: 0 手势传递 点击手势 捏合手势 轻扫手势 拖动手势 长按手势

  • iOS手势操作

    iOS手势有六种 手势类型: 手势状态: 创建View添加手势 1.轻点手势( UITapGestureRecog...

  • Vue手势

    点击手势 滑动手势 手势的方法

  • UI梳理——手势

    手势分类: 手势的创建: 方法的实现: 轻扫手势:UISwipeGestureRecognizer 长按手势: 以...

网友评论

      本文标题:手势

      本文链接:https://www.haomeiwen.com/subject/pzdlottx.html