UI ⼿势识别器UIScreenEdgePanGestureRecognizer

#import "RootViewController.h"

@interface RootViewController ()<UIGestureRecognizerDelegate>

{
    
    CGFloat  _centerX;
    CGFloat  _centerY;
    UIView  *_backgroundView;

}

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 存储坐标
    _centerX = self.view.bounds.size.width / 2;
    _centerY = self.view.bounds.size.height / 2;
    self.view.backgroundColor = [UIColor blackColor];

    // 屏幕边缘pan手势(优先级高于其他手势)
    UIScreenEdgePanGestureRecognizer *leftEdgeGesture = \
    [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self
                                                      action:@selector(handleLeftEdgeGesture:)];
    leftEdgeGesture.edges = UIRectEdgeLeft;           // 屏幕左侧边缘响应
    [self.view addGestureRecognizer:leftEdgeGesture]; // 给self.view添加上
    
    // 设置一个UIView用来替换self.view,self.view用来当做背景使用
    _backgroundView = [[UIView alloc] initWithFrame:self.view.bounds];
    _backgroundView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_backgroundView];
    
    // 展示的view
    UIView *showView_01 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    showView_01.tag = 0x1;
    showView_01.backgroundColor = [UIColor redColor];
    [_backgroundView addSubview:showView_01];
}

- (void)handleLeftEdgeGesture:(UIScreenEdgePanGestureRecognizer *)gesture
{
    // 获取到当前被触摸的view
    UIView *view = [self.view hitTest:[gesture locationInView:gesture.view]
                            withEvent:nil];
    
    NSLog(@"tag = %ld", (long)view.tag);
    
    if(UIGestureRecognizerStateBegan == gesture.state ||
       UIGestureRecognizerStateChanged == gesture.state)
    {
        // 根据被触摸手势的view计算得出坐标值
        CGPoint translation = [gesture translationInView:gesture.view];
        NSLog(@"%@", NSStringFromCGPoint(translation));
        
        NSLog(@"进行中");
        
        // 进行设置
        _backgroundView.center = CGPointMake(_centerX + translation.x, _centerY);
    }
    else
    {
        // 恢复设置
        [UIView animateWithDuration:.3 animations:^{
            _backgroundView.center = CGPointMake(_centerX, _centerY);
            
        }];
    }
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值