我用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 關注
- 170 瀏覽
添加回答
舉報
0/150
提交
取消