iOS基本手势

#import "TouchViewController.h"

@interface TouchViewController ()

@end

@implementation TouchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];

    NSArray *imageArr = [NSArray arrayWithObjects:@"CyanSquare",@"MagentaSquare",@"YellowSquare", nil];
    for (int i = 0; i < 3; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+90*i, 50+110*i, 100, 100)];;
        imageView.image = [UIImage imageNamed:imageArr[i]];
        imageView.userInteractionEnabled = YES;
        [self.view addSubview:imageView];

        // 添加点击手势
        [self addGesture:imageView];
        [imageView release];
    }

}
// 添加手势识别器
- (void)addGesture:(UIImageView *)imageView
{
    // 1、添加单击事件,点击手势识别器
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    // 设置点击的操作
    tap.numberOfTapsRequired = 2;
    // 将手势添加到view 上
    [imageView addGestureRecognizer:tap];
    [tap release];

//    // 2、创建一个移动手势
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
//    [imageView addGestureRecognizer:pan];
//    [pan release];

    // 3、 实现缩放,捏合手势
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinClick:)];
    [imageView addGestureRecognizer:pin];
    [pin release];

    // 4、旋转
    UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rota:)];
    [imageView addGestureRecognizer:rota];
    [rota release];
    // 5、长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpress:)];
    [imageView addGestureRecognizer:longPress];
    [longPress release];

    // 6-1、轻扫手势,一个手势只能支持一个手势,要想支持多个,就得重新创建多个方向的
    // 会和移动的手势冲突
    UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip1.direction = UISwipeGestureRecognizerDirectionUp ;
    [imageView addGestureRecognizer:swip1];
    [swip1 release];
    // 6-2、轻扫手势
    UISwipeGestureRecognizer *swip2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip2.direction = UISwipeGestureRecognizerDirectionDown ;
    [imageView addGestureRecognizer:swip2];
    [swip2 release];
    // 6-3、轻扫手势
    UISwipeGestureRecognizer *swip3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip3.direction = UISwipeGestureRecognizerDirectionLeft ;
    [imageView addGestureRecognizer:swip3];
    [swip3 release];
    // 6-4、轻扫手势
    UISwipeGestureRecognizer *swip4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip4.direction = UISwipeGestureRecognizerDirectionRight ;
    [imageView addGestureRecognizer:swip4];
    [swip4 release];
}

// 实现手势的事件,只有点击图片的时候才会触发
- (void)tapClick:(UITapGestureRecognizer *)sender
{
    [self.view bringSubviewToFront:sender.view];
}

// 实现移动手势,类似于touchMove
- (void)move:(UIPanGestureRecognizer *)sender
{
    // 固定的写法
    CGPoint point = [sender translationInView:self.view];
    sender.view.center = CGPointMake(sender.view.center.x + point.x, sender.view.center.y + point.y);
    [sender setTranslation:CGPointZero inView:self.view];
}

// 实现捏合手势的功能

- (void)pinClick:(UIPinchGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
    sender.scale = 1;

}
// 实现旋转
- (void)rota:(UIRotationGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
    sender.rotation = 1;
}
时间: 2024-10-28 22:49:44

iOS基本手势的相关文章

iOS中手势的delaysTouchesBegan属性用法(挖坑)

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/css/cuteeditor.css);iOS中手势的delaysTouchesBegan属性用法(挖坑),布布扣,bubuko.com

ios各种手势,很有意思

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event  - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent

IOS开发——手势 &amp; 传感器 &amp; 物理引擎

这次思维导图比较杂,demo已经全部上传到github上,小编的github地址是:狂戳 先看下效果图: 手势画板: 物理引擎: 传感器: IOS开发--手势 & 传感器 & 物理引擎

IOS中手势UIGestureRecognizer

通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRotationGestureRecognizer IOS中手势有很多: UIRotationGestureRecognizer旋转 UITapGestureRecognizer手指点击 UIPinchGestureRecognizer缩放 UISwipeGestureRecognizer手指快速扫过 UI

iOS之手势滑动返回功能-b

iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的,那么你需要自定义UINavigationBar,但当你自定义navigationBar后,这个功能就会自动失效. 屏蔽右滑返回功能代码:   if ([self.navigationController respondsToSelector:@selector(interactivePopGest

ios的手势UIGestureRecognizer

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

ios的手势操作之UIGestureRecognizer

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)

【转载】Unity3D研究院之IOS触摸屏手势控制镜头旋转与缩放

前几篇文章介绍了很多Unity3D引擎自身的一些问题, 今天我们在回到IOS设备上讨论一些触摸屏幕手势,本章的目标是通过触摸iPhone屏幕手势 实现模型左右的旋转,与模型的缩放. 大家想一想模型的旋转,实际上是镜头的旋转.模型的缩放实际上是镜头Z轴方向的坐标.那么实现本章的内容只需要控制镜头的位置方可实现. 我们创建一个简单的游戏平面, 然后平面中放一个箱子做为旋转缩放的参照物.如下图所示,选中摄像机,给摄像机添加一个脚本名称为Move. 脚本中有一个参数 Target,它的作用是设置摄像头旋

【iOS开发-手势】iOS中各种手势总结

iOS中支持的手势事件 UITapGestureRecognizer(敲击) UIPinchGestureRecognizer(捏合,用于缩放) UIPanGestureRecognizer(拖拽) UISwipeGestureRecognizer(轻扫) UIRotationGestureRecognizer(旋转) UILongPressGestureRecognizer(长按) UITapGestureRecognizer(敲击) //创建UITapGestureRecognizer对象,

【转】 ios的手势操作之UIGestureRecognizer浅析

一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)