我用UIPanGestureRecognizer加在一個View上用來監(jiān)聽手指的滑動,然后根據(jù) translation 的值來修改view的frame,但是我發(fā)現(xiàn)這樣修改,view的動畫并不能實時反應(yīng),手指滑動快了,就會出現(xiàn)延遲現(xiàn)象。代碼如下:- (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貢獻(xiàn)1780條經(jīng)驗 獲得超1個贊
可以在view中重寫
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
達(dá)成此目的。
另外gesture recognizer正常情況不會出現(xiàn)延遲。請檢查是否每次對位置的提交都發(fā)起了動畫(比如layer.position = newPosition默認(rèn)情況下是有動畫的,在觸摸移動時設(shè)置layer.position且不關(guān)閉動畫,移動會有明顯延遲)
- 1 回答
- 0 關(guān)注
- 183 瀏覽
添加回答
舉報
0/150
提交
取消