我用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];
}慕神8447489
相关分类