[英]UIPanGestureRecognizer - view dissappears on drag
I have a strange problem with using UIPanGestureRecognizer to move a view from the top of the screen. 使用UIPanGestureRecognizer从屏幕顶部移动视图时遇到一个奇怪的问题。 Everything works perfect in simulator but on the actual device the view dissappears when I touch it.
一切都可以在模拟器中完美运行,但是当我触摸它时,在实际设备上的视图便消失了。
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint start;
if (recognizer.state == UIGestureRecognizerStateBegan) {
start = [recognizer translationInView:self.view];
originCenter = recognizer.view.center.y;
}
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:self.view];
if (down) {
if (originCenter + translation.y < self.view.center.y && self.view.center.y + translation.y < self.view.center.y) {
CGPoint move = CGPointMake(self.view.frame.size.width/2, originCenter-(start.y-translation.y));
[recognizer.view setCenter:move];
}
}else{
if (originCenter + translation.y < self.view.center.y && originCenter + translation.y > originCenter) {
CGPoint move = CGPointMake(self.view.frame.size.width/2, originCenter-(start.y-translation.y));
[recognizer.view setCenter:move];
}
}
}
if([recognizer state] == UIGestureRecognizerStateEnded || [recognizer state] == UIGestureRecognizerStateCancelled ){
if(recognizer.view.center.y > -40){
[recognizer.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
down = TRUE;
}else{
[recognizer.view setFrame:CGRectMake(0, 80-self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
down = FALSE;
}
}
}
There is one thing that comes to my mind that there might be problem with calculating the position on the device and the view is moved outside the screen. 我想到的一件事是,计算设备上的位置可能会出现问题,并且视图会移动到屏幕之外。 Is that possible?
那可能吗?
If nothing else, you should make start
a static
or an ivar. 如果没有其他要求,则应将
start
设为static
或ivar。 Otherwise, when you hit UIGestureRecognizerStateChanged
, start
will not have preserved the value it had during UIGestureRecognizerStateBegan
. 否则,当您按下
UIGestureRecognizerStateChanged
, start
将不会保留其在UIGestureRecognizerStateBegan
期间的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.