码迷,mamicode.com
首页 > 移动开发 > 详细

iOS学习必须了解的七大手势

时间:2017-06-29 11:21:04      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:支持   创建对象   ssd   事件   旋转   应该   一个   atom   状态   

文章只要你有一点点基础应该就可以看的懂,文章只为学习交流

#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学习必须了解的七大手势

标签:支持   创建对象   ssd   事件   旋转   应该   一个   atom   状态   

原文地址:http://www.cnblogs.com/Free-Thinker/p/7093120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!