我用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跟着手指运动,还有什么其他的方案吗?还是我这个用法用的不对?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
可以在view中重写
达成此目的。
另外gesture recognizer正常情况不会出现延迟。请检查是否每次对位置的提交都发起了动画(比如layer.position = newPosition默认情况下是有动画的,在触摸移动时设置layer.position且不关闭动画,移动会有明显延迟)