// 實現響應者類中的方法 來捕捉出摸事件
TouchView *touch = [[TouchView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
touch.backgroundColor = [UIColor redColor];
[self.view addSubview:touch];
{
{
// touch 保留手指信息(觸摸的點)
UITouch *touch = [touches anyObject];
// NSLog(@"%@",touch);
//取出當前觸摸的點
//返回一個當前觸摸的點相對於傳進去的參數view
CGPoint p = [touch locationInView:self.window];
NSLog(@"%@",NSStringFromCGPoint(p));
CGPoint p1 = [touch previousLocationInView:self.window] ;
NSLog(@"%@",NSStringFromCGPoint(p1));
CGPoint p2;
p2.x = p.x - p1.x ;
p2.y = p.y - p1.y ;
NSLog(@"%@",NSStringFromCGPoint(self.center));
self.center = CGPointMake(self.center.x + p2.x, self.center.y +p2.y);
self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"觸摸中斷比如小退");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"觸摸結束");
{
NSLog(@"%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
響應者鏈 分爲兩個過程
1.查詢過程
當你點擊屏幕
先定位到 應用程序->window->控制器->self.view->view子視圖一一查找 直到定位被點擊的子視圖 查詢過程結束
2.響應過程
//開始晃動
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"開始晃動");
}
//中止晃動
-(void)motionCancelled:(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];
@property(nonatomic,retain)id target;
@property(nonatomic,assign)SEL action;//方法類型
-(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
{
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(@"點擊");
//使用self.target 對象調用action方法
//讓一個對象去調用這個對象類裏的方法
//object 可攜帶的參數
[self.target performSelector:self.action withObject:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{