iOS编程-------事件处理UIEvent / 触摸事件UITouch

//
//  AppDelegate.h
//  UI04_UIEvent_UITouch
//
//  Created by l on 15/9/4.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end


//
//  AppDelegate.m
//  UI04_UIEvent_UITouch
//
//  Created by l on 15/9/4.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    /*
     UI04 事件, 触摸事件
     知识点
     1.事件 UIEvent 触摸事件 
     touchs event
     2.UITouch
     3.响应者链
     */

    //rootVC
    RootViewController *rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = rootVC;
    [rootVC release];


    return YES;
}

@end




///




//
//  RootViewController.h
//  UI04_UIEvent_UITouch
//
//  Created by l on 15/9/4.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


//
//  RootViewController.m
//  UI04_UIEvent_UITouch
//
//  Created by l on 15/9/4.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "RootViewController.h"
#import "TouchView.h"//导入点击视图头文件
@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建一个点击视图,并且添加到RootVC上
    TouchView *touchView = [[TouchView alloc] initWithFrame:(CGRectMake(150, 100, 75, 100))];

    //是否打开用户交互
    //UIImageView 默认是No,不打开
    //UIView 默认是YES,打开
    //影响用户交互的三个属性
    //1.alpha 为0 不进行用户交互
    //2.hidden yes 不进行用户交互
    //3.userInteractionEnabled为no,不进行用户交互
    //他们的本质都是hitTest方法,返回值为nil



    touchView.backgroundColor = [UIColor redColor];
    [self.view addSubview:touchView];
    [touchView release];


    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end












//
//  TouchView.h
//  UI04_UIEvent_UITouch
//
//  Created by l on 15/9/4.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TouchView : UIView

@end


//
//  TouchView.m
//  UI04_UIEvent_UITouch
//
//  Created by l on 15/9/4.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "TouchView.h"

@implementation TouchView

//响应者链的事件分发机制
//硬件捕获到触摸事件后会一级一级调用视图的hitTest方法和pointInside方法,来查找用户操作的是否是该视图
//1.调用hitText:withEvent:查找第一个响应者,查找触摸点存在的视图,返回该视图,该视图就是第一响应者
//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
如果点击在该视图内返回self,否则返回nil
//    //系统的hitTest 寻找第一响应者方法内,调用了pointInside点是否在视图内方法
//    return self;
//}
//
起一个标记作用,触摸点是否在视图内
如果上面是nil,下面一定是NO,上面是self,下面是YES;
//- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
//    return YES;
//}


/*
 UIView继承自UIResponder响应者类,因此可以检测到用户的相关操作,响应相关的事件,如果想要处理相关触摸事件,必须重写responder类里的几个方法.
 */
//1. 触摸开始(手指刚接触屏幕)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//参数:event 为应用程序,捕获到的事件
//参数:touches 为事件中的所有触摸点,存储在一个集合中
//通过event获取到的allTouches和参数中的touches相同
//    NSSet *allTouches = [event allTouches];
//    if ([allTouchs isEqualToSet:touches]) {
//        NSLog(@"相同的");
//    }
//    NSLog(@"%p", allTouchs);
//    NSLog(@"%p", touches);

//    NSLog(@"%s %d %@", __FUNCTION__, __LINE__, @"触摸开始");
    self.backgroundColor = [self randomColor];

    //点击一下,随机换位置,范围仍在界面内
    //解决思路:改变self.center
    //1.获取self的宽高,确定变换范围
//    CGFloat width = self.frame.size.width;
//    CGFloat heigth = self.frame.size.height;
//    
//    //屏幕总宽度和高度
//    CGFloat totalWidth = self.window.frame.size.width;
//    CGFloat totalHeight = self.window.frame.size.height;
//    
//
//    CGFloat minX = width / 2.0; //坐标x的最小值
//    CGFloat maxX = totalWidth - width / 2.0;//坐标x的最大值
//    
//
//    CGFloat minY = heigth / 2.0; //坐标y的最小值
//    CGFloat maxY = totalHeight - heigth / 2.0;坐标y的最大值
//
//    //创建两个随机数x范围和y范围
//    CGFloat randomX = arc4random() % (int)(maxX - minX + 1) + minX;
//    CGFloat tenMaxY = (maxY - minY + 1) * 10;
//    CGFloat randomY = arc4random() % (int)(tenMaxY) / 10.0 + minY;
//    //随机点
//    CGPoint randomPoint = CGPointMake(randomX, randomY);
//    //改变center的位置
//    self.center = randomPoint;


}

//获取一个随机颜色
- (UIColor *)randomColor{
    CGFloat r = arc4random() % 256/255.0;
    CGFloat g = arc4random() % 256/255.0;
    CGFloat b = arc4random() % 256/255.0;
    UIColor *randomColor = [UIColor colorWithRed:r green:g blue:b alpha:1];
    return randomColor;
}

//2. 触摸移动:手指停留在屏幕上,移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
//    NSLog(@"%s %d %@", __FUNCTION__, __LINE__, @"触摸移动");
//    self.backgroundColor = [self randomColor];

    //1.获取事件的触摸点
    UITouch *touch = [touches anyObject];//touchs为一个集合set,我们通过anyObject获取集合中的一个对象.

    //2.获取触摸点在视图上当前的位置
    CGPoint currentPoint = [touch locationInView:self];
    //获取触摸点在视图上的上一个位置
    CGPoint previousPoint = [touch previousLocationInView:self];

//    NSLog(@"触摸点%@到此", touch);
//    NSLog(@"%@ 当前位置", NSStringFromCGPoint(currentPoint));
//    NSLog(@"%@ 先前位置", NSStringFromCGPoint(previousPoint));

//   3.实现视图跟随手指偏移进行移动
    CGFloat changeX =  currentPoint.x - previousPoint.x; //触摸点x偏移量
    CGFloat changeY = currentPoint.y - previousPoint.y;//触摸点y偏移量
    self.center = CGPointMake(self.center.x + changeX, self.center.y + changeY);
//    self.center = CGPointMake(currentPoint.x, currentPoint.y); //错误的!!!



 //4.实现轻扫效果
    //解决思路:通过判断当前点位置和先前点位置的差值,得到手指移动方向
    //如果x偏移量 > 0 说明向右扫
    //如果y偏移量 > 0 说明向下扫
    //1.获取前后x差值
    CGFloat changeX2 = currentPoint.x - previousPoint.x;
    //2.判断方向,改变位置
    CGFloat rangeMax = self.window.frame.size.width - self.frame.size.width / 2.0;
    CGFloat rangeMin = self.frame.size.width / 2.0;
    if (changeX2 > 0) {
        //视图向右移动50距离
        if (self.center.x < rangeMax - 50) {
            self.center = CGPointMake(self.center.x + 50, self.center.y);
        }
    }else if(changeX2 < 0){
        //否则视图向左移动50距离
        if (self.center.x > rangeMin + 50) {
           self.center = CGPointMake(self.center.x - 50, self.center.y);
        }
    }


}
//3. 触摸结束:手指刚离开屏幕
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s %d %@", __FUNCTION__, __LINE__, @"触摸结束");
}
//4. 触摸取消:当有电话呼入的时候
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s %d %@", __FUNCTION__, __LINE__, @"触摸取消");



}


@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值