UI - iOS事件之TouchEnvent

<AppDelegate.m>

#import "AppDelegate.h"
#import "RootViewController.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.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    RootViewController *rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = rootVC;
    [rootVC release];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


<RootViewController.h>

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


<RootViewController.m>

#import "RootViewController.h"
#import "TounchView.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (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.
    
    //创建 touchView对象
    TounchView *tounchView = [[TounchView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    tounchView.backgroundColor = [UIColor redColor];
    tounchView.layer.cornerRadius = 100;
    [self.view addSubview:tounchView];
    [tounchView release];
    
    
    
    //===================阻断响应者链=============
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 320, 200)];
    label.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:label];
    [label release];
    
    UIButton *bt = [UIButton buttonWithType:UIButtonTypeSystem];
    bt.backgroundColor = [UIColor blueColor];
    [bt setTitle:@"点击" forState: UIControlStateNormal];
    bt.frame = CGRectMake(100, 50, 100, 50);
    //将 button 添加到 label 上
    [label addSubview:bt];
    
    //当label 和 button 交互的时候,点击 button 是没有效果的,因为 label 的用户交互是关闭的,所以打开 UILabel 的用户交互就可以了
    label.userInteractionEnabled = YES;
    

}
//当 touchview 中的touchesBegan不存在时,触摸时触发此方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"我是 controller");
}



- (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


<TounchView.h>

#import <UIKit/UIKit.h>

@interface TounchView : UIView


@end


<TounchView.m>

#import "TounchView.h"

@interface TounchView()
{
    CGPoint _position;  //记录手指的位置
    CGPoint _center;  //记录中心点
}

@end



@implementation TounchView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
         
    }
    return self;
}
/*
 iOS 中的事件包含三种:触摸事件,晃动事件,远程操控事件
 
 1.触摸事件:⽤户通过触摸设备屏幕操作对象、输⼊入数据等,对于每一个事件 UIEvent 对象
 iOS 支持多点触摸,默认是单点触摸
 其实对于视图都能够接受到触摸事件,之所以没有反应 是因为对应的触发方法没有实现.
 touchesBegan:touchesEnd:touchesMove:touchesCancle
 

 */

//当手指触摸视图时触发
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"开始触摸");
    //实现点击改变位置
  //  [self changePosition];
    
    //===================实现拖动视图效果==================
    
    
    //那么我们从集合touches 中使用 anyobject 的对象获取的就是我们的手指
    //获取手指
    UITouch *touch = [touches anyObject];
    //获取手指的位置
    _position = [touch locationInView:self];
    
    _center = self.center;
    
}
//当手指在视图上,并且移动时触发
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"正在触摸");
    
    //实现在视图上移动改变颜色
    //[self changeColor];
    
    
    
    
  //获取手指
    UITouch *touch = [touches anyObject];
    //获取手指的位置
    CGPoint position = [touch locationInView:self];
    //获取手指横向和纵向差值
    CGFloat x = position.x - _position.x;
    CGFloat y = position.y - _position.y;
    
    //获取原来中心点的位置
    CGPoint center = self.center;
    //设置当前的中心点
    self.center = CGPointMake(center.x + x, center.y + y);

    
}
//当手指离开视图时触发
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸结束");
   
    //创建动画
    [UIView beginAnimations:@"position" context:self];
    //设置持续时间
    [UIView setAnimationDuration:0.5];
    //设置更改后的位置
    self.center = _center;
    //提交动画
    [UIView commitAnimations];
    
    
}
//当手指未离开屏幕,但是程序进入后台时触发
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"取消触摸");
    
   
    
}


//改变视图颜色
-(void)changeColor
{
    self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}

//改变视图位置
-(void)changePosition
{
    //一下一段代码是实现了动画的过程
    
    //创建动画
    [UIView beginAnimations:@"position" context:self];
    //设置持续时间
    [UIView setAnimationDuration:0.5];
    //设置重复次数
//    [UIView setAnimationRepeatCount:100];
    //设置更改后的位置
    self.center = CGPointMake(arc4random()% 320, arc4random()% 568);
    //提交动画
    [UIView commitAnimations];
}


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

@end


你可能感兴趣的:(Touch事件,TouchEnvent,iOS事件,触摸事件总结)