25.UITouch

第一响应者和移动视图
myView.m文件

#import "myView.h"
@interface myView()
//用来记录开始的坐标
@property(nonatomic,assign)CGPoint statrPoint;
@end

@implementation myView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    //集合里元素个数,集合里有且只有一个触摸类的对象
    NSLog(@"%ld",touches.count);//--->1
    UITouch *touch = [touches anyObject];
    //通过触摸对象获取相应的视图的当前位置
    self.statrPoint = [touch locationInView:self];
    NSLog(@"%@",NSStringFromCGPoint(_statrPoint));//打印开始点-->{115, 8}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸移动");
    //通过移动,找到变化,然后让myView也进行相应的调整,从而实现视图随手移动的效果
    UITouch *touch = [touches anyObject];
    //获取移动之后的坐标
    CGPoint currentPoint = [touch locationInView:self];
    //坐标变化
    CGFloat X = currentPoint.x - _statrPoint.x;
    CGFloat Y = currentPoint.y - _statrPoint.y;
    //只能通过CGPointMake对center同时进行x,y的赋值
    self.center = CGPointMake(self.center.x + X, self.center.y + Y);
}
@end

MainViewController.m文件

#import "MainViewController.h"
#import "myView.h"

@interface MainViewController ()
@property(nonatomic,retain)UITextField *myTextFiled;
@end

@implementation MainViewController

- (void)loadView
{
    [_myTextFiled release];
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _myTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(110, 40, 150, 40)];
    _myTextFiled.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_myTextFiled];
    [_myTextFiled release];

    //创建一个可移动的myView
    myView *aView = [[myView alloc] initWithFrame:CGRectMake(100, 440, 75, 75)];
    aView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:aView];
    [aView release];
    //创建一个普通的view
    UIView *bview = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200 ,200)];
    bview.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:bview];
    [bview release];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, 100, 40);
    button.backgroundColor = [UIColor redColor];
    [bview addSubview:button];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(150, 150, 100, 100);
    button1.backgroundColor = [UIColor blueColor];
    [bview addSubview:button1];
    [button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)click:(UIButton *)button{
    NSLog(@"----逗比了吧----");
    //让键盘成为第一响应者,注意:点击黄色bview内部的红色button和蓝色button1键盘会弹起,但是点击黄色bview之外的蓝色button1键盘不会弹起.
    [self.myTextFiled becomeFirstResponder];
}
//ViewController中初始化方法,loadView,viewdidLoad只会运行一次,但是viewapper只要视图显示就会运行一次

//下面的七个方法都是UIResponder类自己的方法
//摇一摇
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"摇一摇开始");
    self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"摇一摇结束");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)even{
    NSLog(@"摇一摇被取消");
}

//触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸开始");
    //点击空白回收键盘
    [self.myTextFiled resignFirstResponder];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸移动");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸结束");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸取消");
}

简单的效果如下图,其中的绿色小方块可以随便移动
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值