我用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];
}
1 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
可以在view中重写
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
达成此目的。
另外gesture recognizer正常情况不会出现延迟。请检查是否每次对位置的提交都发起了动画(比如layer.position = newPosition默认情况下是有动画的,在触摸移动时设置layer.position且不关闭动画,移动会有明显延迟)
- 1 回答
- 0 关注
- 132 浏览
添加回答
举报
0/150
提交
取消