IOS_触摸UITouch+手势识别GestureRecognizer+摇一摇+转换点坐标

H:/0801/01_触摸_UITouch_BlueView.h
//
//  BlueView.h
//  输入事件01-UITouch演练
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BlueView : UIView

@end

H:/0801/01_触摸_UITouch_BlueView.m
//
//  BlueView.m
//  输入事件01-UITouch演练
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "BlueView.h"

@implementation BlueView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"点击了蓝色视图");
}

@end

H:/0801/01_触摸_UITouch_greenView.h
//
//  greenView.h
//  触摸事件02-手势识别
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface greenView : UIView

@end

H:/0801/01_触摸_UITouch_greenView.m
//
//  greenView.m
//  触摸事件02-手势识别
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "greenView.h"

@implementation greenView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

#pragma mark - UITouch事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"点击了绿色视图");
}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
//    NSLog(@"绿色视图的HitTest");
//    
//    return [super hitTest:point withEvent:event];
//}

@end

H:/0801/01_触摸_UITouch_MyView.h
//
//  MyView.h
//  输入事件01-UITouch演练
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MyView : UIView

@end

H:/0801/01_触摸_UITouch_MyView.m
//
//  MyView.m
//  输入事件01-UITouch演练
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
//        UIButton *button = [
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"点击了红色视图");
}


@end

H:/0801/01_触摸_UITouch_ViewController.h
//
//  ViewController.h
//  输入事件01-UITouch演练
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// 演示视图
@property (weak, nonatomic) IBOutlet UIView *demoView;

@end

H:/0801/01_触摸_UITouch_ViewController.m
//
//  ViewController.m
//  输入事件01-UITouch演练
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // 在编写代码的时候,可以通过程序去控制UIResponser是否接受用户交互,
    // 以及是否支持多点触摸
    [self.view setUserInteractionEnabled:YES];
    [self.view setMultipleTouchEnabled:YES];
    
    
}
#pragma mark - UITouch事件
#pragma mark 触摸开始,很难保存同时接触屏幕,参数1set里常为1个touch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 通过NSLog,我们不难发现,接收到集合中存储的是UITouch的集合
    // 集合属性中,包括了,触摸所在的窗口、视图、当前点击位置、上一次点击位置
//    NSLog(@"触摸数据集合:%@", touches);
    
    // 如何获取到用户的点击,可以使用NSSet anyObject方法
//    UITouch *touch = [touches anyObject];
//    NSLog(@"触摸开始Touch对象个数:%d",[touches count]);
    // 完整的UITouch事件调试方法
    NSLog(@"触摸开始");
    for (UITouch *touch in touches) {
        NSLog(@"%@", touch);
    }
}

#pragma mark 触摸移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸移动Touch对象个数:%d",[touches count]);
    // 要移动界面上黄颜色的视图
    
    // 1. 得到当前手指的位置
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    // 2. 得到上一次手指的位置
    CGPoint preLocation = [touch previousLocationInView:self.view];
    // 3. 计算两个位置之间的偏移
    CGPoint offset = CGPointMake(location.x - preLocation.x, location.y - preLocation.y);
    // 4. 使用计算出来的偏移量,调整视图的位置
    [_demoView setCenter:CGPointMake(_demoView.center.x + offset.x, _demoView.center.y + offset.y)];
    
    // 完整的UITouch事件调试方法
    NSLog(@"触摸移动");
    for (UITouch *touch in touches) {
        NSLog(@"%@", touch);
    }
}

#pragma mark 触摸结束,很难保存同时离开,参数1set里常为1个touch
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 完整的UITouch事件调试方法
    NSLog(@"触摸完成");
    for (UITouch *touch in touches) {
        NSLog(@"%@", touch);
    }
}

#pragma mark 触摸中断
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 完整的UITouch事件调试方法
    NSLog(@"触摸中断");
    for (UITouch *touch in touches) {
        NSLog(@"%@", touch);
    }
}

@end

H:/0801/02_手势识别...GestureRecognizer_ViewController.h
//
//  ViewController.h
//  输入事件02-手势识别演示
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// 演示视图
@property (weak, nonatomic) IBOutlet UIView *demoView;

@end

H:/0801/02_手势识别...GestureRecognizer_ViewController.m
//  ViewController.m
//  输入事件02-手势识别演示
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];    
    // 手势识别通常是在视图加载的时候定义的
    //    创建手势识别实例
    //    设置手势识别属性,例如手指数量,方向等
    //    将手势识别附加到指定的视图之上
    //    编写手势触发响应方法
    // 注意:不要去直接实例化UIGestureRecognizer,需要根据自己的需要,实例化具体的手势
    /* 1. 演示点按手势*/
    // 根据实例化方法,我们知道:
    // 1.有一个处理消息的对象,应该是self
    // 2.我们需要定义一个方法,当手势识别检测到的时候,运行
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
			initWithTarget:self action:@selector(tapAction:)];
    // setNumberOfTapsRequired 点按次数
    [tap setNumberOfTapsRequired:1];
    // setNumberOfTouchesRequired 点按的手指数量
    [tap setNumberOfTouchesRequired:1];
    // 把手势识别增加到视图上
    [self.demoView addGestureRecognizer:tap];
    /**
     2. 捏合点按手势
     */
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
			initWithTarget:self action:@selector(pinchAction:)];
    [self.demoView addGestureRecognizer:pinch];
    /**
     3. 旋转点按手势
     */
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]
			initWithTarget:self action:@selector(rotationAction:)];
    [self.demoView addGestureRecognizer:rotation];
}
#pragma mark - 旋转手势
- (void)rotationAction:(UIRotationGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
    // 和捏合操作类似,旋转角度同样需要方复位,下次捏合时,再以当前的图像为基础旋转
    sender.rotation = 0.0f;
}
#pragma mark - 捏合手势
- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    // 有关转换的内容,我们在后续动画部分再继续
    sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
    // 缩放功能很简单,但是要将比例复位,最后下次捏合时,再以当前的图像为基础缩放
    sender.scale = 1.0f;
    NSLog(@"捏我了");
}
#pragma mark - 点按手势
- (void)tapAction:(UITapGestureRecognizer *)sender
{
    NSLog(@"点我了 %@", sender);
}
@end

H:/0801/03_手势识别...GestureRecognizer_ViewController.hViewController.h
//
//  ViewController.h
//  输入事件02-手势识别演示
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// 演示视图
@property (weak, nonatomic) IBOutlet UIView *demoView;

// 测试imageView
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

H:/0801/03_手势识别...GestureRecognizer_ViewController.mViewController.m
//
//  ViewController.m
//  输入事件02-手势识别演示
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    // 手势识别通常是在视图加载的时候定义的
    //    创建手势识别实例
    //    设置手势识别属性,例如手指数量,方向等
    //    将手势识别附加到指定的视图之上
    //    编写手势触发响应方法
    //
    // 注意:不要去直接实例化UIGestureRecognizer,需要根据自己的需要,实例化具体的手势
    /**
     1. 演示点按手势
     */
    // 根据实例化方法,我们知道:
    // 1.有一个处理消息的对象,应该是self
    // 2.我们需要定义一个方法,当手势识别检测到的时候,运行
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
				initWithTarget:self action:@selector(tapAction:)];
    // setNumberOfTapsRequired 点按次数
    [tap setNumberOfTapsRequired:1];
    // setNumberOfTouchesRequired 点按的手指数量
    [tap setNumberOfTouchesRequired:1];
    // 把手势识别增加到视图上
    [self.demoView addGestureRecognizer:tap];
    
    /**
     2. 捏合点按手势
     */
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
				initWithTarget:self action:@selector(pinchAction:)];
    [self.demoView addGestureRecognizer:pinch];
    
    /**
     3. 旋转点按手势
     */
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]
				initWithTarget:self action:@selector(rotationAction:)];
    [self.demoView addGestureRecognizer:rotation];
    
    /**
     4. 拖放点按手势
     */
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]
				initWithTarget:self action:@selector(panAction:)];
    [self.demoView addGestureRecognizer:pan];
    
    /**
     5. 长按手势
     */
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
				initWithTarget:self action:@selector(longPressAction:)];
    [self.demoView addGestureRecognizer:longPress];
    
    /**
     6. 轻扫手势
     
     关于轻扫手势,是需要指定方向的,如果你不指定方向,那么只能接收到的向右方向的轻扫事件
     */
    // 向左扫
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
					initWithTarget:self action:@selector(swipeAction:)];
	//设置扫的方向
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.view addGestureRecognizer:swipeLeft];
    // 向右扫
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
				initWithTarget:self action:@selector(swipeAction:)];
    //设置扫的方向
	[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.view addGestureRecognizer:swipeRight];
    // 向上扫
    UISwipeGestureRecognizer *swipeTop = [[UISwipeGestureRecognizer alloc]
				initWithTarget:self action:@selector(swipeAction:)];
    //设置扫的方向
	[swipeTop setDirection:UISwipeGestureRecognizerDirectionUp];
    [self.view addGestureRecognizer:swipeTop];
    // 向下扫
    UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc]
				initWithTarget:self action:@selector(swipeAction:)];
    //设置扫的方向
	[swipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
    [self.view addGestureRecognizer:swipeDown];
}
#pragma mark - 轻扫手势
- (void)swipeAction:(UISwipeGestureRecognizer *)sender
{
    NSLog(@"%d", sender.direction);
    switch (sender.direction) {
        case UISwipeGestureRecognizerDirectionLeft:
            NSLog(@"向左扫");
            break;
        case UISwipeGestureRecognizerDirectionRight:
            NSLog(@"向右扫");
            break;
        case UISwipeGestureRecognizerDirectionUp:
            NSLog(@"向上扫");
            break;
        case UISwipeGestureRecognizerDirectionDown:
            NSLog(@"向下扫");
            break;
        default:
            break;
    }
}

#pragma mark - 长按手势
- (void)longPressAction:(UILongPressGestureRecognizer *)sender
{
    // 我们可以利用demoView的Tag属性,默认时tag=0
    // 如果tag=0,我们放大一倍,否则,我们缩小一半
    CGFloat scale;
    if (_demoView.tag == 0) {
        scale = 2.0;
        _demoView.tag = 1;
    } else {
        scale = 0.5;
        _demoView.tag = 0;
    }
    
    sender.view.transform = CGAffineTransformScale(sender.view.transform, scale, scale);
}

#pragma mark - 拖放手势
- (void)panAction:(UIPanGestureRecognizer *)sender
{
    // 在拖放手势中是需要考虑手指的状态的UIGestureRecognizerState
    // 在拖放手势中使用的状态是UIGestureRecognizerStateChanged
    // 通常在使用拖放手势的时候,当手指离开的时候,应该做一个很小的动作,提醒用户拖放完成
    if (sender.state == UIGestureRecognizerStateChanged) {
        // locationInView
		//中心变成触摸点位置
        [_demoView setCenter:[sender locationInView:self.view]];
    } else if (sender.state == UIGestureRecognizerStateEnded) {
		//结束时,给个提示
        [_demoView setBackgroundColor:[UIColor yellowColor]];
    }
}

#pragma mark - 旋转手势
- (void)rotationAction:(UIRotationGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
    
    // 和捏合操作类似,旋转角度同样需要复位,下次再以这次为基础
    sender.rotation = 0.0f;
}

#pragma mark - 捏合手势
- (void)pinchAction:(UIPinchGestureRecognizer *)sender
{
    // 有关转换的内容,我们在后续动画部分再继续
    sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
    
    // 缩放功能很简单,但是不要忘记将比例复位,下次再以这次为基础
    sender.scale = 1.0f;
    NSLog(@"捏我了");
}

#pragma mark - 点按手势
- (void)tapAction:(UITapGestureRecognizer *)sender
{
    /**
     在开发过程中,如果没有什么必要,最好不要对一个UI控件,既使用触摸,又使用手势。
     */
    NSLog(@"点我了 %@", sender);
}

#pragma mark - 手势触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸事件!");
    // 1. 先取出UITouch对象
    // 2. 判断响应点击的UIView是不是我们需要的
    UITouch *touch = [touches anyObject];
    if ([touch view] == _imageView) {
        NSLog(@"点到图像了!");
    }
}

@end

H:/0801/04_摇一摇_UIEventSubtypeMotionShake_ShakeListenerView.h
//
//  ShakeListenerView.h
//  输入事件03-手机摇晃
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ShakeListenerView : UIView

@end

H:/0801/04_摇一摇_UIEventSubtypeMotionShake_ShakeListenerView.m
//
//  ShakeListenerView.m
//  输入事件03-手机摇晃
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "ShakeListenerView.h"

@implementation ShakeListenerView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
//因为UIView默认只是个容器,不能成为第一响应者的,
//重写该方法,使之能够成为第一响应者
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

@end

H:/0801/04_摇一摇_UIEventSubtypeMotionShake_ViewController.h
//
//  ViewController.h
//  输入事件03-手机摇晃
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

H:/0801/04_摇一摇_UIEventSubtypeMotionShake_ViewController.m
//  ViewController.m
//  输入事件03-手机摇晃
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark - 要让ViewController支持摇晃,需要写三个方法
// 1. 成为第一响应者,视图一出现时,就应该成为第一响应者
- (void)viewDidAppear:(BOOL)animated
{
    [self.view becomeFirstResponder];
    // 千万不要忘记去实现父类方法~~~~~~~~
    [super viewDidAppear:animated];
}
// 2. 注销第一响应者,视图要关闭的时候,注销
- (void)viewDidDisappear:(BOOL)animated
{
    [self.view resignFirstResponder];
    // 千万不要忘记去实现父类方法~~~~~~~~
    [super viewDidDisappear:animated];
}
// 3. 监听并处理移动事件,判断是否摇晃了手机
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (motion == UIEventSubtypeMotionShake) {
        NSLog(@"摇啊摇,摇到外婆桥!!!");
    }
}
@end

H:/0801/05_转换点坐标_pointInside+convertPoint_ViewController.h
//
//  ViewController.h
//  输入事件04-触摸事件特殊处理
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

H:/0801/05_转换点坐标_pointInside+convertPoint_ViewController.m
//
//  ViewController.m
//  输入事件04-触摸事件特殊处理
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@end

H:/0801/05_转换点坐标_pointInside+convertPoint_YellowView.h
//
//  YellowView.h
//  输入事件04-触摸事件特殊处理
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface YellowView : UIView

@property (weak, nonatomic) IBOutlet UIButton *button;

@end

H:/0801/05_转换点坐标_pointInside+convertPoint_YellowView.m
//  YellowView.m
//  输入事件04-触摸事件特殊处理
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "YellowView.h"
@implementation YellowView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
#pragma mark - 触摸事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"点击黄色视图了");
}
// 是Window用来递归查找最适合响应的视图的
//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//{
//}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{    
    // 结论1:参数point是相对当前视图左上角的坐标,而当前视图覆盖住了按钮
    // 如果,我们能够取出被黄色视图覆盖住的按钮范围,就可以解决问题
    // 我们取出按钮相对point的位置,然后调用按钮的pointIndside方法去判断
    // point是否在按钮的范围上
    // buttonPoint是相对按钮所在视图的点
	//转换从当前view中坐标系的一个点坐标,到目标控件坐标系的坐标
    CGPoint buttonPoint = [_button convertPoint:point fromView:self];
    if ([_button pointInside:buttonPoint withEvent:event]) {
        // 把视图的pointInside方法终止,交由按钮响应点击事件
        return NO;
    }
    // 父类方法要确保能够正常执行
    return [super pointInside:point withEvent:event];
}
@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值