手勢(shì)
@property (nonatomic, strong) UIView *testView;
///記錄旋轉(zhuǎn)角度
@property (nonatomic, assign) CGFloat rotation;
///記錄縮放比例
@property (nonatomic, assign) CGFloat scale;
// !!!: 點(diǎn)擊
- (void)addTapGestureRecognizer {
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer:)];
[self.view addGestureRecognizer:tapGes];
}
- (void)tapGestureRecognizer:(UITapGestureRecognizer *)sender {
NSLog(@"點(diǎn)擊");
}
// !!!: 長(zhǎng)按
- (void)addLongPressGestureRecognizer {
UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGestureRecognizer:)];
//用幾個(gè)手指觸屏愧驱,默認(rèn)1,設(shè)置多少就必須多少觸碰點(diǎn)
longPressGes.numberOfTouchesRequired = 1;
//設(shè)置最短長(zhǎng)按時(shí)間审孽,單位為秒(默認(rèn)0.5)
longPressGes.minimumPressDuration = 1;
//設(shè)置手勢(shì)識(shí)別期間所允許的手勢(shì)可移動(dòng)范圍,默認(rèn)10
longPressGes.allowableMovement = 10;
[self.view addGestureRecognizer:longPressGes];
}
- (void)longPressGestureRecognizer:(UILongPressGestureRecognizer *)sender {
// CGPoint point = [sender locationInView:self.view];
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"UIGestureRecognizerStateBegan");
}else if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"UIGestureRecognizerStateChanged");
}else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"UIGestureRecognizerStateEnded");
}
//其他沒啥用
}
// !!!: 快速滑動(dòng)
- (void)addSwipeGestureRecognizer {
UISwipeGestureRecognizer *leftSwipeGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizer:)];
leftSwipeGes.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipeGes];
UISwipeGestureRecognizer *rightSwipeGes = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizer:)];
rightSwipeGes.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipeGes];
}
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)sender {
NSLog(@"%@",@(sender.direction));
}
// !!!: 慢速滑動(dòng)
- (void)addPanGestureRecognizer {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizer:)];
[self.view addGestureRecognizer:panGesture];
}
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)sender {
CGPoint point = [sender translationInView:self.view];
if (ABS(point.x) > ABS(point.y)*2) {//30度
if (point.x > 10) {//速度
}else if (point.x < -10) {
}
}
//每次調(diào)用之后拭宁,需要重置手勢(shì)的偏移量聚假,否則偏移量會(huì)自動(dòng)累加
[sender setTranslation:CGPointZero inView:self.view];
}
// !!!: 邊緣滑動(dòng)
- (void)addScreenEdgePanGestureRecognizer {
UIScreenEdgePanGestureRecognizer * edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureRecognizer:)]; //手勢(shì)由self來管理
edgePan.edges = UIRectEdgeRight;
[self.view addGestureRecognizer:edgePan];
}
- (void)screenEdgePanGestureRecognizer:(UIScreenEdgePanGestureRecognizer *)sender {
CGFloat progress = fabs([sender translationInView:[UIApplication sharedApplication].keyWindow].x / [UIApplication sharedApplication].keyWindow.bounds.size.width);
NSLog(@"%@-%@",@(sender.state).description,@(progress).description);
}
// !!!: 縮放
- (void)addPinchGestureRecognizer {
UIPinchGestureRecognizer *gesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureRecognizer:)];
// scale
[self.view addGestureRecognizer:gesture];
self.testView.backgroundColor = [UIColor greenColor];
}
- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)sender {
CGFloat scale = sender.scale;
scale = self.scale * scale;
if (sender.state == UIGestureRecognizerStateEnded) {
self.scale = scale;
}
self.testView.transform = CGAffineTransformMake(scale, 0, 0, scale, 0, 0);
}
// !!!: 旋轉(zhuǎn)
- (void)addRotationGestureRecognizer {
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureRecognizer:)];
// rotation: //獲取旋轉(zhuǎn)角度
// velocity: //獲取旋轉(zhuǎn)速度
[self.view addGestureRecognizer:rotationGesture];
self.testView.backgroundColor = [UIColor greenColor];
}
- (void)rotationGestureRecognizer:(UIRotationGestureRecognizer *)sender {
// 獲取手勢(shì)旋轉(zhuǎn)的弧度
CGFloat rotation = sender.rotation;
rotation += self.rotation;
if (sender.state == UIGestureRecognizerStateEnded) {
self.rotation = rotation;
while (self.rotation < -M_PI*2) {
self.rotation += M_PI*2;
}
while (self.rotation > M_PI*2) {
self.rotation -= M_PI*2;
}
}
self.testView.transform = CGAffineTransformMakeRotation(rotation);
}
- (UIView *)testView
{
if (!_testView) {
_testView = [UIView newAutoLayoutView];
[self.view addSubview:_testView];
[_testView autoCenterInSuperview];
[_testView autoSetDimensionsToSize:CGSizeMake(50, 50)];
}
return _testView;
}
- (CGFloat)scale
{
if (_scale == 0) {
_scale = 1;
}
return _scale;
}