iOS开发之手势交互

    //全部直接或者间接继承自UIResponder(就是继承自UIViewUIViewController数组

    //),均可以去重写UITouch的相关方法,去检查开始触摸,结束触摸以及移动等事件app



#import "AppDelegate.h"ide

#import "FJViewController.h"spa

@interface AppDelegate ().net


@end 3d


@implementation AppDelegateorm



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {对象

    // Override point for customization after application launch.继承

    

     _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];事件

    

    [_window setBackgroundColor:[UIColor whiteColor]];

    

    

    //全部直接或者间接继承自UIResponder(就是继承自UIViewUIViewController

    //),均可以去重写UITouch的相关方法,去检查开始触摸,结束触摸以及移动等事件

    

    //=========================================

    


    FJViewController *viewc = [[FJViewController alloc]init];

    _window.rootViewController = viewc;

    

    

    [_window makeKeyAndVisible];

    


    return YES;

}

@end


FJLabel.m


#import "FJLabel.h"


@implementation FJLabel


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

    

    

    

    UITouch *touch = [touches anyObject];

    

    CGPoint point = [touch locationInView:self.superview];

    

    //label跟着移动,从新设置当前label的坐标

    self.center = point;

    

    

}


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

    

    [UIView animateWithDuration:0.01 animations:^{

        

        self.transform = CGAffineTransformMakeScale(0, 0) ;

        

    }];

}




@end



FJViewController.m


#import "FJLabel.h"

#import "FJViewController.h"


@interface FJViewController (){

    UITextField *field;

    UIImageView *imageView;

}


@end


@implementation FJViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    //设置背景颜色

    self.view.backgroundColor = [UIColor redColor];

    

    //UITouch的应用:点击屏幕任何其余的地方收起键盘

    //重写touchBegain方法在这个方法中收起键盘

    

   field = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];

    field.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:field];

    

#pragma mark - UITouch在视图控制器中拖动一个视图

    

    

    imageView  = [[UIImageView alloc]

                              initWithFrame:CGRectMake(100, 250, 50, 50)];

    

    imageView.backgroundColor = [UIColor cyanColor];

    [self.view addSubview:imageView];

    

    

#pragma mark -UITouch移动一个label

    

    FJLabel *lable= [[FJLabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];

    lable.text = @"hello world";

    lable.userInteractionEnabled = YES;

    [self.view addSubview:lable];

    

}








#pragma mark UITouch的基本知识

//开始触摸当前视图的时候调用这个方法


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

    

     //NSSet

    //参数1:当前的触摸对象集合:NSSet的功能和数组基本同样用来同时存储多个对象

    //和数组的区别是:NSArray中的数组元素是有序的,可是NSSet中的元素是无序的

    

    //参数2:事件

    //1.拿到当前的触摸对象

    UITouch *touch = [touches anyObject];

    

    //2.拿到当前触摸的坐标

    CGPoint point = [touch locationInView:self.view];

    

    NSLog(@"%@",NSStringFromCGPoint(point));

    

    //3.拿到当前全部的触摸点(可能同时拿到多个触摸对象)

    NSSet *set = event.allTouches;

    //4.只能经过forin去遍历(多个手指同时触摸)

    for (UITouch *tTouch in set) {

        

        CGPoint point = [tTouch locationInView:self.view];

        NSLog(@"%@",NSStringFromCGPoint(point));

    }

    

    

    NSLog(@"开始触摸");

#pragma mark - UITouch的应用

    //==================UITouch的应用:收起键盘=======================

    [field resignFirstResponder];

    

    //==================应用二:拖动视图==============

    

    imageView.center = point;

}



//触摸结束调用方法

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

    

    NSLog(@"触摸结束");

    //拿到触摸结束时得touch对象

    UITouch *touch = [touches anyObject ];

    //拿到触摸结束的坐标

    CGPoint point = [touch locationInView:self.view];

    NSLog(@"%@",NSStringFromCGPoint(point));

    

    

}


//这方法会在手指按住不放的过程当中时时调用

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

    

    NSLog(@"移动");

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.view];

    //==================应用二:拖动视图==============

    imageView.center = point;

    

    

    NSLog(@"%@",NSStringFromCGPoint(point));

}






- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




@end

相关文章
相关标签/搜索