ios – UIGestureRecognizer拖动UIView

ios – UIGestureRecognizer拖动UIView,第1张

概述我正在尝试构建一个GestureRecognizer来从右到左拖动一个UIView.如果用户将视图拖动到屏幕的一半,则视图将被自动拖动到左上角放弃,但如果用户不拖动到屏幕的一半,它将返回到屏幕右侧的角落. 有些迷失在这里,任何教程还是什么? 谢谢 编辑:在UIScrollView里面有一些代码,它的一个UIImageView,我需要拖动整个滚动条,或者拖动它的内部图像: _myScroll = [ 我正在尝试构建一个GestureRecognizer来从右到左拖动一个UIVIEw.如果用户将视图拖动到屏幕的一半,则视图将被自动拖动到左上角放弃,但如果用户不拖动到屏幕的一半,它将返回到屏幕右侧的角落.
有些迷失在这里,任何教程还是什么?
谢谢

编辑:在UIScrollVIEw里面有一些代码,它的一个UIImageVIEw,我需要拖动整个滚动条,或者拖动它的内部图像:

_myScroll = [[UIScrollVIEw alloc] initWithFrame:CGRectMake(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,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];

并且我尝试拖动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];}
解决方法 看看 here.这是一个UIGestureRecognizer教程,它将为您提供处理捏和平移手势的基础知识.一旦了解了基础知识,就可以很容易地完成你想要的工作.所有您需要的是在平移完成后检查视图的位置以将其发送到正确的位置.看看UIScrollVIEws上的 this other tutorial,它可能会帮助你找到你的方式通过第二部分.

这两个教程来自raywenderlich.com,可能是我认识的最有用的iOS教程网站.

以下是您可以处理的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];}

我不会给你完美的答案在这里,你必须在代码上找到一种方法,使其工作,你想要它.

这是最后一个提示.您可以使用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

然后在UIVIEw animateWithDuration :,使用slIDeFactor作为持续时间.

总结

以上是内存溢出为你收集整理的ios – UIGestureRecognizer拖动UIView全部内容,希望文章能够帮你解决ios – UIGestureRecognizer拖动UIView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1096973.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存