UIPanGestureRecognizer 和 UIPinchGestureRecogizer 用于放大和移动反弹-拉回动画
Posted
技术标签:
【中文标题】UIPanGestureRecognizer 和 UIPinchGestureRecogizer 用于放大和移动反弹-拉回动画【英文标题】:UIPanGestureRecognizer and UIPinchGestureRecogizer for zoom in AND move with bounce-pull back animation 【发布时间】:2014-06-25 11:57:59 【问题描述】:我在 UIView 中有 UIImageView。 请假设用户只能放大(不在此代码中)。 如果 uiimage 比 UIView 大,我需要拉回 uiimage 的动画,就像在 facebook 应用程序中一样。
意思是,如果用户将 UIImageView 向上移动..当他抬起手指时,UIImageView 将被拉回以覆盖底部的空白空间。
我试着玩它,但是..没有运气。 提前致谢
-(void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
CGFloat scale = [recognizer scale];
[recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];
[recognizer setScale:1.0];
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
CGPoint translation = [recognizer translationInView:recognizer.view];
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
【问题讨论】:
【参考方案1】:如果您的图像大于其容器,则应该可以使用:
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
CGPoint translation = [recognizer translationInView:recognizer.view];
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
else if(state==UIGestureRecognizerStateEnded)
UIView *imageView = recognizer.view;
UIView *container = imageView.superview;
CGFloat targetX = CGRectGetMinX(imageView.frame);
CGFloat targetY = CGRectGetMinY(imageView.frame);
if(targetX>0)
targetX = 0;
else if(CGRectGetMaxX(imageView.frame)<CGRectGetWidth(container.bounds))
targetX = CGRectGetWidth(container.bounds)-CGRectGetWidth(imageView.frame);
if(targetY>0)
targetY = 0;
else if(CGRectGetMaxY(imageView.frame)<CGRectGetHeight(container.bounds))
targetY = CGRectGetHeight(container.bounds)-CGRectGetHeight(imageView.frame);
[UIView animateWithDuration:0.3 animations:^
imageView.frame = CGRectMake(targetX, targetY, CGRectGetWidth(imageView.frame), CGRectGetHeight(imageView.frame));
];
【讨论】:
感谢您的帮助。我测试你的代码。放大后,我移动了 uiimageview,但现在不能移动太多,也不能移动到边框。它只停留在中心区域。您的代码在放大后考虑 uiimageview 的新大小? 显然不是 ;) 所以尝试将viewSize.width
和 viewSize.height
与您当前的 scaleFactor 相乘
不是最好的,但一个简单的解决方案是在每次更改时将比例存储在变量中。如何将比例分配给 UIImageView?
我更新了我的问题。请检查。这是我的错误。当 UIImageView 小于 superview 时,您的代码很好。但是,我的意思是别的。 UIImageView 比 superview 大。当用户结束移动/平移手势时,会有动画拉回图像以覆盖空白空间(因为 uiimageview 比 superview 更大)。 facebook 应用程序 (ipad) 和其他应用程序具有此回拉动画。对不起,我错了。如果您仍然可以提供帮助,请参阅我更新的问题。谢谢!以上是关于UIPanGestureRecognizer 和 UIPinchGestureRecogizer 用于放大和移动反弹-拉回动画的主要内容,如果未能解决你的问题,请参考以下文章
UISegmentedControl 和 UIPanGestureRecognizer?
一种使用 UIPinchGestureRecognizer UIRotationGestureRecognizer 和 UIPanGestureRecognizer 添加类似 Instagram 的布
如何在 UIPangestureRecognizer 上禁用多点触控?
在 UILongPressGestureRecognizer 上启用 UIPanGestureRecognizer
识别同一 UIButton 上的 UILongPressGestureRecognizer 和 UIPanGestureRecognizer
UIPanGestureRecognizer 和 UIPinchGestureRecogizer 用于放大和移动反弹-拉回动画