[TwistedFate]触摸事件 晃动事件 target/action

触摸事件的方法

创建TouchView类 并在根视图控制器里初始化
类里 添加触摸事件响应方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"开始摸");
    // UITouch保存手指的信息 例如触摸的点
//    UITouch *touch = [touches anyObject];
//    //NSLog(@"%@",touch);
//    
//    // 取出当前触摸的点 相当于传进去的参数View
//    CGPoint p1 = [touch locationInView:self];

//    NSLog(@"%@",NSStringFromCGPoint(p1));

    // 返回当前点的上一个点 相对于传进去的参数View
//    CGPoint p2 = [touch previousLocationInView:self];
//    NSLog(@"%@",NSStringFromCGPoint(p2));
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"移动中");
    // 取出当前点
    // 取出当前点上一个点
    // 计算X轴的偏移量
    // 计算Y轴的偏移量
    // 偏移量
    UITouch *touch = [touches anyObject];
    CGPoint p1 = [touch previousLocationInView:self];
    CGPoint p2 = [touch locationInView:self];
    CGFloat x = p2.x - p1.x;
    CGFloat y = p2.y - p1.y;
    self.center = CGPointMake(self.center.x + x, self.center.y + y);
    CGFloat blue = (arc4random() % 256 / 255.0);
    CGFloat red = (arc4random() % 256 / 255.0);
    CGFloat green = (arc4random() % 256 / 255.0);
    self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:(arc4random() % 2 / 1.0)];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"抚摸结束");
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸被中断,例如触摸中 来电话可以中断,小退出");
}

晃动事件

// 晃动事件
// 开始晃动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"开始晃动了");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"结束晃动");
    // 跳转界面
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    // 跳转
    // 模态视图跳转
    // 无层级跳转 *******
    [self presentViewController:secondVC animated:YES completion:nil];
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"中断晃动");
}

target/action

使UIView拥有类似于UIButton的方法
创建ButtonView类
// buttonView.h文件

@property (nonatomic, retain) id target;
@property (nonatomic, assign) SEL action;
- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action;

// buttonView.m文件

- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action{
    self = [super initWithFrame:frame];
    if (self) {
        self.target = target;
        self.action = action;
    }
    return self;
}



// 开始触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"开始");


}
// 移动触摸
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸中");

}
// 结束触摸
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"点击结束");
    // 使用self.target对象调用action方法
    // 让一个对象去掉用类里的方法
    // Object 可携带的参数 ******
    [self.target performSelector:self.action withObject:self];
}
// 中断触摸
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"中断");
}

RootViewController.m文件

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ButtonView *buttonView = [[ButtonView alloc] initWithFrame:CGRectMake(100, 100, 100, 100) target:self action:@selector(buttonViewClicked:)];
    buttonView.backgroundColor = [UIColor redColor];
    [self.view addSubview:buttonView];
    [buttonView release];
}
- (void)buttonViewClicked:(UIView *)view{
    view.backgroundColor = [UIColor cyanColor];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值