触摸,摇一摇,遥控
打开或关闭用户交互属性
// 打开或关闭用户交互属性
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__);
}