UI03事件处理

触摸,摇一摇,遥控


打开或关闭用户交互属性

    // 打开或关闭用户交互属性
    self.actionView.userInteractionEnabled = NO;


    // 触发事件
    ButtonView *buttonView = [[ButtonView alloc] initWithFrame:CGRectMake(20, 100, 280, 280)];
    [self.view addSubview:buttonView];
    [buttonView release];

// 给图片添加个相框
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 100, 280, 280)];
    imageView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:imageView];
    [imageView release];

添加图片

    //UIImage 图片类,一个对象代表一张图片
    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    
    // 在UIImageView上显示图片
    imageView.image = image;


系统调用触摸方法

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

// 移动触摸
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __func__);
    
    // 1,获得UITouch对象
    UITouch *touch = [touches anyObject];
    
    // 获得这次触摸经过的在self.view上的点(坐标)
    CGPoint point = [touch locationInView:self.view];
    
    if (touch.view == _actionView) {
        self.actionView.backgroundColor = [UIColor redColor];
        // 让View的中心点和手指经过的点一样
        _actionView.center = point;
    }

}

// 触摸结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __func__);

}

系统调用摇一摇

// 摇一摇开始
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 在UIview中,这个方法不会被触发
    NSLog(@"约");
    NSLog(@"%s", __func__);
    

}

// 摇一摇结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"%s", __func__);

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值