觸摸事件的方法
創建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];
}