目錄
1.1 點(diǎn)擊手勢
1.2 長按手勢
1.3 移動手勢
1.4 旋轉(zhuǎn)手勢
1.5 縮放手勢
1.6 方向滑動手勢
1.1 點(diǎn)擊手勢
//輕觸 彩蛋
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
//觸摸次數(shù)
tap.numberOfTapsRequired = 2;
//觸摸點(diǎn)數(shù)量
//tap.numberOfTouchesRequired = 2;
[imageView addGestureRecognizer:tap];
1.2 長按手勢
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[imageView addGestureRecognizer:longPress];
//長按事件
- (void)longPress:(UILongPressGestureRecognizer*)longPress{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"開始長按");
}
if (longPress.state == UIGestureRecognizerStateChanged) {
NSLog(@"長按中");
}
if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"長按結(jié)束");
}
}
1.3 移動手勢
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[imageView addGestureRecognizer:pan];
//移動事件
- (void)pan:(UIPanGestureRecognizer*)pan{
CGPoint point = [pan translationInView:self.view];
imageView.center = CGPointMake(imageView.center.x + point.x, imageView.center.y + point.y);
//以當(dāng)前點(diǎn)為原點(diǎn)計(jì)算
[pan setTranslation:CGPointZero inView:self.view];
}
1.4 旋轉(zhuǎn)手勢
UIRotationGestureRecognizer* rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
rotation.delegate=self;
[imageView addGestureRecognizer:rotation];
- (void)rotation:(UIRotationGestureRecognizer*)rotation{
_imageView.transform = CGAffineTransformMakeRotation(_rotation + rotation.rotation);
if (rotation.state == UIGestureRecognizerStateEnded) {
_rotation += rotation.rotation;
}
}
1.5 縮放手勢
UIPinchGestureRecognizer* pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
pinch.delegate=self;
[imageView addGestureRecognizer:pinch];
- (void)pinch:(UIPinchGestureRecognizer*)pinch{
//1.1 1.1
_imageView.bounds = CGRectMake(0, 0, _imageView.bounds.size.width * pinch.scale, _imageView.bounds.size.height * pinch.scale);
[pinch setScale:1];
}
1.6 方向滑動手勢
需要分開寫
self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:self.leftSwipeGestureRecognizer];
[self.view addGestureRecognizer:self.rightSwipeGestureRecognizer];
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
}
if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。