手勢與觸控,手勢觸控
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 75, 75)];
imgView.image=[UIImage imageNamed:@"a"];
imgView.tag=200;
[self.view addSubview:imgView];
//設定允許觸控和執行手勢
imgView.userInteractionEnabled=YES;
//建立點選手勢
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];
//將建立的點選手勢添加到圖片對象
[imgView addGestureRecognizer:tap];
//建立拖拽手勢
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureClick:)];
[imgView addGestureRecognizer:pan];
//建立長按手勢
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureClick:)];
[imgView addGestureRecognizer:longPress];
//建立旋轉手勢
UIRotationGestureRecognizer *rotate=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotateGestureClick:)];
[imgView addGestureRecognizer:rotate];
//建立捏合手勢
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureClick:)];
//捏合手勢可以與其他手勢同時執行
pinch.delegate=self;
[imgView addGestureRecognizer:pinch];
//建立橫掃和縱掃手勢
UISwipeGestureRecognizer *hs=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(hsClick:)];
hs.direction=UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:hs];
UISwipeGestureRecognizer *vs=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(vsClick:)];
vs.direction=UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:vs];
}
-(void)vsClick:(UISwipeGestureRecognizer *)vs
{
NSLog(@"縱掃");
}
-(void)hsClick:(UISwipeGestureRecognizer *)hs
{
NSLog(@"橫掃");
}
//當執行捏合手勢時調用該方法
-(void)pinchGestureClick:(UIPinchGestureRecognizer *)pinch
{
pinch.view.transform=CGAffineTransformScale(pinch.view.transform, pinch.scale,pinch.scale);
[pinch setScale:1];
}
//當執行旋轉手勢時調用該方法
-(void)rotateGestureClick:(UIRotationGestureRecognizer *)rotate
{
//roate.rotation手指的旋轉弧度
rotate.view.transform=CGAffineTransformRotate(rotate.view.transform, rotate.rotation);
//保持圖片和手指為相同弧度
[rotate setRotation:0];
}
//當執行長按時調用該方法
//參數名稱.view為被添加手勢的對象
-(void)longPressGestureClick:(UILongPressGestureRecognizer*)loginPress
{
loginPress.view.center=self.view.center;
}
//當執行拖拽手勢時執行該方法
-(void)panGestureClick:(UIPanGestureRecognizer*)pan
{
//pan.view為手指點擊的對象
pan.view.center= [pan locationInView:self.view];
}
//參數為圖片的手勢類型
-(void)tapGestureClick:(UITapGestureRecognizer *)tap
{
NSLog(@"tap");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//return YES,允許添加手勢的對象多個手勢同時執行
//遵守UIGestureRecognizerDelegate協議
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
(觸控)
//添加圖片到頁面上
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 75, 75)];
imgView.image=[UIImage imageNamed:@"a"];
[self.view addSubview:imgView];
imgView.tag=100;
//允許當前螢幕支援多點觸控
self.view.multipleTouchEnabled=YES;
//允許圖片對象支援手勢與觸控
imgView.userInteractionEnabled=YES;
//設定導航條右側按鈕
UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick:)];
self.navigationItem.rightBarButtonItem=rightBtn;
}
-(void)rightBtnClick:(UIBarButtonItem*)bt
{
_secondController=[[SecondViewController alloc]init];
_secondController.navigationItem.title=@"Gesture";
[self.navigationController pushViewController:_secondController animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//觸控螢幕時調用該方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touhesBegan");
//獲得螢幕上的手指個數
NSLog(@"%i",[touches count]);
}
//當手指在螢幕上滑動調用該方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UIImageView *imgView=(UIImageView *)[self.view viewWithTag:100];
for (UITouch *t in touches) {
//t.view獲得手指點擊的空件對象
if (t.view==imgView) {
//圖片根據點到圖片上的手指的位置而移動
imgView.center=[t locationInView:self.view];
}
}
NSLog(@"moving");
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"end");
}