事件处理和手势

 

事件处理和手势

 

 

【视图与UITouch对应的方法】

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //获取点击事件

    UITouch *t = [touches anyObject];

    //如果点击的是图片就把他移到视图的最上层

    if ([t.view isKindOfClass:[UIImageView class]]) {

        [self.view bringSubviewToFront:t.view];

    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *t = [touches anyObject];

    //如果点击的是图片就让他移动

    if ([t.view isKindOfClass:[UIImageView class]]) {

        CGPoint p = [t locationInView:self.view];

        t.view.center = p;

    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *t = [touches anyObject];

    //点到第二次的时候,如果点击的是图片就让图片变长

    if ([t.view isKindOfClass:[UIImageView class]]) {

        if (t.tapCount == 2) {

            CGRect rect = t.view.frame;

            rect.size.height += 10;

            t.view.frame = rect;

        }

    }

}

 

 

【UIGestureRecognizer】手势识别器

6种手势

UITapGestureRecognizer  Tap(点一下)

UIPanGestureRecognizer Pan (拖移,移动)
UIPinchGestureRecognizer Pinch(缩放)
UIRotationGestureRecognizer Rotation(旋转)
UILongPressGestureRecognizer  LongPress(长按)
UISwipeGestureRecognizer Swipe(滑动,轻扫)


- (void)tapGR:(UITapGestureRecognizer *)tapGR

{

    NSLog(@"点一下");

    [self.view bringSubviewToFront:tapGR.view];

}

 

- (void)panGR:(UIPanGestureRecognizer *)panGR

{

    NSLog(@"拖动");

    CGPoint translation = [panGR translationInView:self.view];

    panGR.view.center = CGPointMake(panGR.view.center.x + translation.x,panGR.view.center.y + translation.y);

    [panGR setTranslation:CGPointZero inView:self.view];

}

 

- (void)pinchGR:(UIPinchGestureRecognizer *)pinchGr

{

    NSLog(@"缩放");

    pinchGr.view.transform = CGAffineTransformScale(pinchGr.view.transform, pinchGr.scale, pinchGr.scale);

    pinchGr.scale = 1;

}

 

- (void)rotationGR:(UIRotationGestureRecognizer *)rotationGR

{

    NSLog(@"旋转");

    rotationGR.view.transform = CGAffineTransformRotate(rotationGR.view.transform, rotationGR.rotation);

    rotationGR.rotation = 0;

}

 

- (void)lpGR:(UILongPressGestureRecognizer *)lpGR

{

    NSLog(@"长按");

    if (lpGR.state == UIGestureRecognizerStateEnded) {

 

        CABasicAnimation  *shake=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

//可以写transform.rotation.x   transform.rotation.y  transform.scale

 

        shake.fromValue=[NSNumber numberWithFloat:-0.1];

        shake.toValue=[NSNumber numberWithFloat:+0.1];

        //是否自动返回

        shake.autoreverses=YES;

        //单次持续时间

        shake.duration=0.2;

        //重复次数

        shake.repeatCount=15;

        [lpGR.view.layer addAnimation:shake forKey:@"imageShake"];

 

//取消动画

//[lpgr.view.layer removeAnimationForKey:@"imageShake"];

    }

}

 

-(void)swipe:(UISwipeGestureRecognizer*)swipeGesture

{

    NSLog(@"轻扫,与拖动互斥");

    if (swipeGesture.direction == UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"dowm");

    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionUp) {

        NSLog(@"up");

    } else if (swipeGesture.direction == UISwipeGestureRecognizerDirectionLeft) {

        NSLog(@"left");

    } else {

        NSLog(@"right");

    }

}

 

 

转场动画

 

XXViewController *xvc = [[XXXViewController alloc]init];

 

    CATransition *ani = [CATransition animation];

    ani.duration = 0.8f;

 

//类型

    ani.type = kCATransitionMoveIn;

 

1.#define定义的常量 (基本型)

     kCATransitionFade   交叉淡化过渡 

kCATransitionMoveIn 新视图移到旧视图上面 

     kCATransitionPush   新视图把旧视图推出去 

     kCATransitionReveal 将旧视图移开,显示下面的新视图 

 

 2.用字符串表示

fade moveIn push reveal 和上面的四种一样

     pageCurl             向上翻一页 

     pageUnCurl           向下翻一页 

     rippleEffect       滴水效果 

     suckEffect           收缩效果,如一块布被抽走 

     cube  alignedCube      立方体效果 

     flip  alignedFlip oglFlip  翻转效果  

  rotate 旋转

  cameraIris cameraIrisHollowOpen cameraIrisHollowClose 相机

 

 

    //新视图先从哪个部分出现

    ani.subtype = kCATransitionFromTop;

//四种预设,某些类型中此设置无效

kCATransitionFromRight

kCATransitionFromLeft

kCATransitionFromTop

kCATransitionFromBottom

 

    [self.navigationController.view.layer addAnimation:ani forKey:@"animation"];

    [self.navigationController pushViewController:fvc animated:NO];

 

 

 

 

- (UIImage *)clipImage:(UIImage *)image inRect:(CGRect)rect

{//返回image中rect范围内的图片

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);

    UIImage *subImage = [UIImage imageWithCGImage:imageRef];

    return subImage;

}

#import "ViewController.h"

 

@interface ViewController ()

 

{

    CGPoint _beginPoint;

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    NSArray *imageArr = [NSArray arrayWithObjects:@"CyanSquare", @"MagentaSquare", @"YellowSquare", nil];

    

    for (int i = 0; i<3; i++) {

        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(40+40*i, 60+60*i, 100, 100)];

        iv.image = [UIImage imageNamed:imageArr[i]];

        iv.userInteractionEnabled = YES;

        [self.view addSubview:iv];

    }

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //找到触摸手势

    UITouch *touch = [touches anyObject];

    

    //判断触摸到的view是否属于UIImageView类

    if ([touch.view isKindOfClass:[UIImageView class]])

    {

        //声明一个图片视图的指针,指向触摸到的view

//        UIImageView *iv = (UIImageView *)touch.view;

//        iv.image = [UIImage imageNamed:@"king2"];

        

        [self.view bringSubviewToFront:touch.view];

        

        //刚一点击的时候记录手势的坐标

        _beginPoint = [touch locationInView:self.view];

    }

}

 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    //找到触摸手势

    UITouch *momo = [touches anyObject];

    

    if ([momo.view isKindOfClass:[UIImageView class]]) {

        

        CGPoint newPoint = [momo locationInView:self.view];

        

        //x方向和y方向移动的距离

        CGFloat xMove = newPoint.x - _beginPoint.x;

        CGFloat yMove = newPoint.y - _beginPoint.y;

        

        //触摸到的view中心点按照手势的移动轨迹来移动

        momo.view.center = CGPointMake(momo.view.center.x + xMove, momo.view.center.y + yMove);

        

        //修改记录

        _beginPoint = newPoint;

    }

}

 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *momo = [touches anyObject];

    

    //连续点击的次数

    NSLog(@"===%d",momo.tapCount);

    

    if ([momo.view isKindOfClass:[UIImageView class]]) {

        if (momo.tapCount%2) {

            CGRect rect = momo.view.frame;

            rect.size.height += 10;

            rect.origin.y -= 5;

            momo.view.frame = rect;

        }

    }

}

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    NSArray *imageArr = [NSArray arrayWithObjects:@"CyanSquare", @"MagentaSquare", @"YellowSquare", nil];

    

    for (int i = 0; i<3; i++) {

        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(40+40*i, 60+60*i, 100, 100)];

        iv.image = [UIImage imageNamed:imageArr[i]];

        iv.userInteractionEnabled = YES;

        [self.view addSubview:iv];

        

        [self addGestureRecognizerForView:iv];

    }

}

 

- (void)addGestureRecognizerForView:(UIView *)view

{

    //点击手势

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];

    //添加手势

    [view addGestureRecognizer:tapGR];

    

    //拖拽手势

//    UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];

//    [view addGestureRecognizer:panGR];

    

    //捏合手势(缩放)

    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGR:)];

    [view addGestureRecognizer:pinchGR];

    

    //旋转

    UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGR:)];

    [view addGestureRecognizer:rotationGR];

    

    //长摁

    UILongPressGestureRecognizer *lpGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lpGR:)];

    [view addGestureRecognizer:lpGR];

    

    //轻扫

    UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGR:)];

    //设置轻扫手势的方向

    swipeGR.direction = UISwipeGestureRecognizerDirectionDown;

    [view addGestureRecognizer:swipeGR];

    

    //如果想让一个view能监控2个方向的轻扫,需要创建2次

    UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGR:)];

    leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;

    [view addGestureRecognizer:leftSwipe];

}

 

- (void)swipeGR:(UISwipeGestureRecognizer *)swipeGR

{//轻扫手势(和拖拽手势有冲突)

    CGRect frame = swipeGR.view.frame;

    

    //判断方向,修改frame

    if (swipeGR.direction == UISwipeGestureRecognizerDirectionDown) {

        frame.size.height += 10;

    }

    

    /*

                0000010

                0000010

                0000010

     */

    if (swipeGR.direction & UISwipeGestureRecognizerDirectionLeft) {

        frame.size.width += 10;

        frame.origin.x -= 10;

    }

    

    swipeGR.view.frame = frame;

}

 

- (void)lpGR:(UILongPressGestureRecognizer *)lpGR

{//长按

    //长按手势会多次触发这个事件,需要判断状态

    if (lpGR.state == UIGestureRecognizerStateBegan) {

        

        //基础动画

        CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

        

        //动画的起始和结束值

        basicAnimation.fromValue = [NSNumber numberWithFloat:0.5];

        basicAnimation.toValue = [NSNumber numberWithFloat:1.5];

        

        //单次动画的时间

        basicAnimation.duration = 0.05;

        

        //重复次数

        basicAnimation.repeatCount = 99;

        

        //动画方式返回

        basicAnimation.autoreverses = YES;

        

        [lpGR.view.layer addAnimation:basicAnimation forKey:@"aa"];

    }

}

 

- (void)rotationGR:(UIRotationGestureRecognizer *)rotationGR

{//旋转

    //让手势对应的view根据手势旋转的角度发生改变

    rotationGR.view.transform = CGAffineTransformRotate(rotationGR.view.transform, rotationGR.rotation);

    

    //每次改变以后需要还原记录

    rotationGR.rotation = 0;

}

 

- (void)pinchGR:(UIPinchGestureRecognizer *)pinchGR

{

    //让手势对应的view根据手势捏合的倍数发生改变

    pinchGR.view.transform = CGAffineTransformScale(pinchGR.view.transform, pinchGR.scale, pinchGR.scale);

    

    //每次改变以后要还原记录

    pinchGR.scale = 1;

}

 

- (void)panGR:(UIPanGestureRecognizer *)panGR

{//拖拽手势

    //手势移动的轨迹

    CGPoint translation = [panGR translationInView:self.view];

    

    //点击到的view随着手势移动

    panGR.view.center = CGPointMake(panGR.view.center.x + translation.x, panGR.view.center.y + translation.y);

    

    //每次移动后都需要清空手势所记录轨迹

    [panGR setTranslation:CGPointZero inView:self.view];

}

 

- (void)tapGR:(UITapGestureRecognizer *)tapGR

{//点击手势

    //将点击到的view层级移到最上层

    [self.view bringSubviewToFront:tapGR.view];

}

 

#import "AppDelegate.h"

#import "FirstViewController.h"

 

@implementation AppDelegate

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    

    FirstViewController *fvc = [[FirstViewController alloc] init];

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:fvc];

    self.window.rootViewController = nc;

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}

 

#import "FirstViewController.h"

#import "SecondViewController.h"

 

@interface FirstViewController ()

 

@end

 

@implementation FirstViewController

 

- (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 redColor];

    NSArray *imageArr = [NSArray arrayWithObjects:@"CyanSquare", @"MagentaSquare", @"YellowSquare", nil];

    

    for (int i = 0; i<3; i++) {

        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(40+60*i, 100+120*i, 100, 100)];

        iv.image = [UIImage imageNamed:imageArr[i]];

        iv.userInteractionEnabled = YES;

        [self.view addSubview:iv];

    }

    

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    SecondViewController *svc = [[SecondViewController alloc] init];

    

    //创建一个过场动画

    CATransition *transition = [CATransition animation];

    

    //动画时间

    transition.duration = 0.3;

    

    //动画方向

    transition.subtype = kCATransitionFromRight;

    

    //动画类型

    transition.type = @"suckEffect";

    

    /*

     fade moveIn push reveal 和系统自带的四种一样

          pageCurl             向上翻一页

          pageUnCurl           向下翻一页

          rippleEffect       滴水效果

          suckEffect           收缩效果,如一块布被抽走

          cube  alignedCube      立方体效果

          flip  alignedFlip oglFlip  翻转效果

     rotate 旋转

     cameraIris cameraIrisHollowOpen cameraIrisHollowClose 相机

     */

    

    //添加动画

    [self.navigationController.view.layer addAnimation:transition forKey:nil];

    

    [self.navigationController pushViewController:svc animated:NO];

}

 

#import "SecondViewController.h"

 

@interface SecondViewController ()

 

@end

 

@implementation SecondViewController

 

- (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 yellowColor];

    

    UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"king2"]];

    iv.center = self.view.center;

    [self.view addSubview:iv];

}

 

让明天,不后悔今天的所作所为
原文地址:https://www.cnblogs.com/-yun/p/4366958.html