ios - 用UIPanGestureRecognizer监听滑动延迟
高洛峰
高洛峰 2017-04-17 11:18:51
[iOS讨论组]

我用UIPanGestureRecognizer加在一个View上用来监听手指的滑动,然后根据 translation 的值来修改view的frame,但是我发现这样修改,view的动画并不能实时反应,手指滑动快了,就会出现延迟现象。
代码如下:

- (void)slidePanAction:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:self.movingView];
    if(recognizer.state == UIGestureRecognizerStateChanged) { // sliding.
        self.movingView.frame = CGRectMake(self.origin.x + translation.x, self.origin.y + translation.y,
                                           self.movingView.frame.size.width, self.movingView.frame.size.height);
    }
    else if(recognizer.state == UIGestureRecognizerStateEnded) { // end slide.
        self.origin = CGPointMake(self.movingView.frame.origin.x, self.movingView.frame.origin.y);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.movingView = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)];
    self.movingView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.movingView];
    self.origin = CGPointMake(10.0f, 10.0f);

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(slidePanAction:)];
    [self.view addGestureRecognizer:panRecognizer];
}

如果我要做这种view跟着手指运动,还有什么其他的方案吗?还是我这个用法用的不对?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
巴扎黑

可以在view中重写

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

达成此目的。

另外gesture recognizer正常情况不会出现延迟。请检查是否每次对位置的提交都发起了动画(比如layer.position = newPosition默认情况下是有动画的,在触摸移动时设置layer.position且不关闭动画,移动会有明显延迟)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号