ios UIGestureRecognizer 拖动 UIView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13879666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIGestureRecognizer to Drag UIView
提问by darkman
I'm trying to build a GestureRecognizer
to drag a UIView
from right to left. If the user drag the View to the half of the screen, the View will be automatically dragged into the left corner to discard it, but if the user do not drag up to half the screen it will return to the corner right of the screen.
Little lost here, any tutorials or something?
Thanks
我正在尝试构建一个从右向左GestureRecognizer
拖动一个UIView
。如果用户将View拖到屏幕的一半,View会自动拖到左角丢弃它,但如果用户没有拖到屏幕的一半,它会回到屏幕的右角。小失落,有教程之类的吗?谢谢
EDIT :Heres some code, its a UIImageView
, inside a UIScrollView
, i need to drag the entire scroll or just the image inside it :
编辑:这是一些代码,它是 a UIImageView
,在 a 中UIScrollView
,我需要拖动整个滚动条或仅拖动其中的图像:
_myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, self.window.bounds.size.height)]; [_myScroll setContentSize:CGSizeMake(self.window.bounds.size.width , 1300)];
_tutorial = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width, 1300)]; [_tutorial setImage:[UIImage imageNamed:@"Default"]];
_tutorial.contentMode = UIViewContentModeScaleAspectFit; [_myScroll addSubview:_tutorial];
_tutorial.userInteractionEnabled = YES;
UIPanGestureRecognizer * recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:_tutorial action:@selector(handlePan:)];
[_tutorial addGestureRecognizer:recognizer];
[self.window addSubview:_myScroll];
And the method i try to drag the UIImageView
我尝试拖动的方法 UIImageView
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:_myScroll];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:_myScroll];
}
回答by rdurand
Have a look over here. This is a UIGestureRecognizer
tutorial which will give you the basics to handle pinch and pan gestures. Once you've learnt the basics, it's really easy to accomplish what you want. All you need is to check the position of the view once the pan is completed to send it to the correct position. Have a look at this other tutorialon UIScrollViews
, it may help you find your way through the second part.
看看在这里。这是一个UIGestureRecognizer
教程,它将为您提供处理捏合和平移手势的基础知识。一旦您了解了基础知识,就很容易完成您想要的工作。您只需要在平底锅完成后检查视图的位置,将其发送到正确的位置即可。查看关于 的其他教程UIScrollViews
,它可能会帮助您找到第二部分的方法。
Both tutorials come from raywenderlich.com, probably the most useful iOS tutorials site I know.
这两个教程都来自raywenderlich.com,这可能是我所知道的最有用的 iOS 教程站点。
Here is some code on your handlePan:
method you can work on :
这是handlePan:
您可以使用的方法的一些代码:
- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:_myScroll];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
if (recognizer.state == UIGestureRecognizerStateEnded) {
// Check here for the position of the view when the user stops touching the screen
// Set "CGFloat finalX" and "CGFloat finalY", depending on the last position of the touch
// Use this to animate the position of your view to where you want
[UIView animateWithDuration: aDuration
delay: 0
options: UIViewAnimationOptionCurveEaseOut
animations:^{
CGPoint finalPoint = CGPointMake(finalX, finalY);
recognizer.view.center = finalPoint; }
completion:nil];
}
[recognizer setTranslation:CGPointMake(0, 0) inView:_myScroll];
}
I won't give you the perfect answer here, you'll have to work on the code to find a way to make it work as you want it to.
我不会在这里给你完美的答案,你必须对代码进行工作,找到一种方法让它按照你想要的方式工作。
Here is one last tip. You can do some kind of "smooth" animation by using a slideFactor. It will help your animation being more "realistic". Work on this code, that you add right at the beginning of the "if":
这是最后一个提示。您可以使用 slideFactor 来制作某种“平滑”动画。它将帮助您的动画更加“逼真”。处理此代码,您将其添加到“if”的开头:
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
float slideFactor = 0.1 * slideMult; // Increase for more slide
Then in UIView animateWithDuration:
, use slideFactor
as the duration.
然后在 UIView 中animateWithDuration:
,slideFactor
用作持续时间。