关键词:
【中文标题】在iOS中的iPad上捏缩放将图像移动到最左角?【英文标题】:Pinch zoom shifting image to most left corner on iPad in iOS? 【发布时间】:2016-01-30 17:14:14 【问题描述】:我在 iOS 中有一张图片。当我捏住它移动到左上角的图像时,我在图像上添加了捏合手势。我还在图像上添加了平移手势。当图像被缩放时,我会向各个方向滚动图像,为此我已将平移手势添加到图像中。
我的代码是:
-(void)viewDidLoad
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[self.zoom_image addGestureRecognizer:pinch];
panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[self.zoom_image addGestureRecognizer:panGesture];
img_center_x = self.zoom_image.center.x;
img_center_y = self.zoom_image.center.y;
-(void)handlePinch:(UIPinchGestureRecognizer*)sender
NSLog(@"latscale = %f",mLastScale);
mCurrentScale += [sender scale] - mLastScale;
mLastScale = [sender scale];
NSLog(@"before ceneter x %f",img_center_x);
NSLog(@"before ceneter x %f",img_center_y);
CGPoint img_center = CGPointMake(img_center_x, img_center_y);
self.zoom_image.center = img_center;
if (sender.state == UIGestureRecognizerStateEnded)
mLastScale = 1.0;
if(mCurrentScale<1.0)
mCurrentScale=1.0;
if(mCurrentScale>3.0)
mCurrentScale=3.0;
CGAffineTransform currentTransform = CGAffineTransformIdentity;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform,mCurrentScale, mCurrentScale);
self.zoom_image.transform = newTransform;
平移手势
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[self.zoom_image addGestureRecognizer:panGesture];
move image:
- (void)moveImage:(UIPanGestureRecognizer *)recognizer
CGPoint translation = [recognizer translationInView:self.zoom_image];
CGPoint location = [recognizer locationInView:self.view];
CGPoint initial=CGPointZero;
NSLog(@"%f\n%f",translation.x,translation.y);
NSLog(@"%f",self.zoom_image.frame.origin.y);
CGPoint finalpoint = CGPointMake(self.zoom_image.center.x + translation.x, self.zoom_image.center.y+ translation.y);
NSLog(@"%f",finalpoint.y);
//limit the boundary
if(recognizer.state==UIGestureRecognizerStateChanged)
if ((self.zoom_image.frame.origin.x>0 && translation.x > 0) || (self.zoom_image.frame.origin.x + self.zoom_image.frame.size.width<=self.view.frame.size.width && translation.x < 0))
finalpoint.x = self.zoom_image.center.x;
if ((self.zoom_image.frame.origin.y>100 && translation.y > 0) || (self.zoom_image.frame.origin.y + self.zoom_image.frame.size.height<=self.view.frame.size.height && translation.y < 0))
finalpoint.y = self.zoom_image.center.y;
//set final position
NSLog(@"%f",finalpoint.y);
self.zoom_image.center = finalpoint;
[recognizer setTranslation:initial inView:self.zoom_image];
【问题讨论】:
【参考方案1】:这是一个可能的解决方案。
• 我已将您的 zoom_image 重命名为 contentView,因为此类可以操作任何视图,而不仅仅是图像。
• 我已经删除了绑定测试,并将比例设为 (0.01 - 10.0)
• 最多三个手指的捏合手柄,也可用作平移。可以在不中断捏合的情况下更改触摸次数。
还有很多需要改进的地方,但主要的原则在这里:)
接口(如 minScale、maxScale、minMargin 等属性仍有待添加 - 为什么不是委托)
@interface PinchViewController : UIViewController
@property(nonatomic,strong) IBOutlet UIView* contentView;
@end
实施
@implementation PinchViewController
CGPoint translation;
CGFloat scale;
CGAffineTransform scaleTransform;
CGAffineTransform translateTransform;
CGPoint previousTranslation;
CGFloat previousScale;
NSUInteger previousNumTouches;
-(void)viewDidLoad
scale = 1.0f;
scaleTransform = CGAffineTransformIdentity;
translateTransform = CGAffineTransformIdentity;
previousTranslation = CGPointZero;
previousNumTouches = 0;
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[self.view addGestureRecognizer:pinch];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panGesture];
-(void)handlePinch:(UIPinchGestureRecognizer*)recognizer
// 1 - find pinch center
CGPoint mid = [self computePinchCenter:recognizer];
mid.x-= recognizer.view.bounds.size.width / 2.0f;
mid.y-= recognizer.view.bounds.size.height / 2.0f;
// 2 - compute deltas
NSUInteger numTouches = recognizer.numberOfTouches;
if ( (recognizer.state==UIGestureRecognizerStateBegan) || ( previousNumTouches != numTouches ) )
previousScale = recognizer.scale;
previousTranslation = mid;
previousNumTouches = numTouches;
CGFloat deltaScale = ( recognizer.scale - previousScale ) * scale;
previousScale = recognizer.scale;
CGPoint deltaTranslation = CGPointMake(mid.x-previousTranslation.x, mid.y-previousTranslation.y);
previousTranslation = mid;
deltaTranslation.x/=scale;
deltaTranslation.y/=scale;
// 3 - apply
scale+=deltaScale;
if (scale<0.01) scale = 0.01; else if (scale>10) scale = 10;
scaleTransform = CGAffineTransformMakeScale(scale, scale);
[self translateBy:deltaTranslation];
NSLog(@"Translation : %.2f,%.2f - Scale Center : %.2f,%.2f - Scale : %.2f",deltaTranslation.x,deltaTranslation.y,mid.x,mid.y,scale);
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
if (recognizer.state==UIGestureRecognizerStateBegan) previousTranslation = CGPointZero;
CGPoint recognizerTranslation = [recognizer translationInView:self.contentView];
CGPoint deltaTranslation = CGPointMake(recognizerTranslation.x - previousTranslation.x,recognizerTranslation.y - previousTranslation.y);
previousTranslation = recognizerTranslation;
[self translateBy:deltaTranslation];
NSLog(@"Translation : %.2f,%.2f - Scale : %.2f",deltaTranslation.x,deltaTranslation.y,scale);
-(void)translateBy:(CGPoint)delta
translation.x+=delta.x;
translation.y+=delta.y;
translateTransform = CGAffineTransformMakeTranslation(translation.x,translation.y);
self.contentView.transform = CGAffineTransformConcat(translateTransform,scaleTransform);
-(CGPoint)computePinchCenter:(UIPinchGestureRecognizer*)recognizer
// 1 - handle up to 3 touches
NSUInteger numTouches = recognizer.numberOfTouches;
if (numTouches>3) numTouches = 3;
// 2 - Find fingers middle point - with (0,0) being the center of the view
CGPoint pt1,pt2,pt3,mid;
switch (numTouches)
case 3:
pt3 = [recognizer locationOfTouch:2 inView:recognizer.view];
case 2:
pt2 = [recognizer locationOfTouch:1 inView:recognizer.view];
case 1:
pt1 = [recognizer locationOfTouch:0 inView:recognizer.view];
switch (numTouches)
case 3:
mid = CGPointMake( ( ( pt1.x + pt2.x ) / 2.0f + pt3.x ) / 2.0f, ( ( pt1.y + pt2.y ) / 2.0f + pt3.y ) / 2.0f );
break;
case 2:
mid = CGPointMake( ( pt1.x + pt2.x ) / 2.0f, ( pt1.y + pt2.y ) / 2.0f );
break;
case 1:
mid = CGPointMake( pt1.x, pt1.y);
break;
return mid;
@end
希望它会有所帮助:) 干杯
【讨论】:
对……这是一个快速的猜测。我已经睡过了。我用一种可能的方式进行编辑和替换。 是的 - 第一个解决方案是快速猜测,但不起作用。编辑后的答案有效,并且仍然允许很多改进。 有哪些改进? 使用诸如 minScale、maxScale 之类的属性设置限制... 捏合算法已经很好地工作了,但是在缩放时手指的中心并不完全静止。它应该,但我不能花更多的时间在它上面:) 顺便说一句,我投票赞成你的问题,因为那是一个有趣的挑战;)再见!在滚动视图内的 UIImageView 上捏缩放效果?
...建照片库查看器。这就是我的imageViewController在情节提要中的设置方式:我已确保在imageView和scrollView上启用userInteracti 查看详情
iOS5 中的图像移动和缩放问题
】iOS5中的图像移动和缩放问题【英文标题】:ImagemoveandscaleissueiniOS5【发布时间】:2011-10-1810:07:20【问题描述】:最近我将我的iphone升级到了iOS5。升级到iOS5后,图像上通常的移动和缩放功能不起作用(UImagePickerController)。在iOs5... 查看详情
UIScrollView 在 iOS7 上捏合时返回错误
】UIScrollView在iOS7上捏合时返回错误【英文标题】:UIScrollViewreturnserroronpinchoniOS7【发布时间】:2015-06-1808:13:44【问题描述】:我正在使用UIScrollView来缩放图像:overridefuncviewDidLoad()super.viewDidLoad()self.scrollView.minimumZoomScale=0.5self.scroll... 查看详情
如何在Android活动栏的最左角添加可点击图标?
】如何在Android活动栏的最左角添加可点击图标?【英文标题】:HowtoaddclickableicontoleftmostcornerofactivitybaronAndroid?【发布时间】:2016-04-2409:47:59【问题描述】:我想知道如何在操作栏的角落创建一个可点击的图标。我想出了如何在... 查看详情
如何在 cocos2d 中平滑移动精灵
】如何在cocos2d中平滑移动精灵【英文标题】:howtodosmoothmovementofspritesincocos2d【发布时间】:2013-07-0710:02:01【问题描述】:在我的cocos2d项目中,我使用CCMoveBy动作将精灵从最左角移动到最右角。这是代码CCSprite*sprite1=[CCSpritespriteWit... 查看详情
如何在捏合和缩放时找出地图的中心点
...心点。我找不到找到中心点(纬度,经度)的逻辑。图像中的红色值表示纬度和经度值。任何人都喜欢提出想法。谢谢你,巴拉提。答案你也可以calculatethetilenumberbasedonlon/lat,然后convertthetilenumberbacktolon/lat。这将告诉您瓷砖的左... 查看详情
ios开发uislider设置图片滑块不能滑到最左最右边
...0;UISlider设置 thumbTintColor 滑块可以正常滑到最左最右边但是设置 ThumbImage 滑块不能滑到最左最右边解决办法:新建类,继承 UISlider在.m 重写 -(CGRect)thumbRectForBounds:(CGRect)boundstrackRect:(CGRec... 查看详情
如何限制将捏缩放图像移动到图像边框?
】如何限制将捏缩放图像移动到图像边框?【英文标题】:Howtorestrictshiftingofapinchzoomedimagetotheimageborders?【发布时间】:2015-11-1714:35:10【问题描述】:我在情节提要中设置了一个scrollView,其中包含一个imageView,使用自动布局,如... 查看详情
在 iPad 和 iPhone 上缩放时背景图像模糊
】在iPad和iPhone上缩放时背景图像模糊【英文标题】:BackgroundimageblurrywhenscalingoniPadandiPhone【发布时间】:2011-08-0720:53:53【问题描述】:我正在创建一个网络应用程序,允许用户缩放某些100x100像素的背景图像。当他们双击图像时... 查看详情
如何使用 iOS 13 在 iPad/iPhone 上隐藏页面缩放栏
...是用Ionic/Angular构建的。现在,每当我们切换到应用程序中的另一个页面时,顶部的白条就会出现,您可以在其中更改页面缩放并访问其他设置(在移动和桌面网站之 查看详情
在单个视图上捏放大多个图像 (Swift)
】在单个视图上捏放大多个图像(Swift)【英文标题】:PinchZoomintomultipleImagesonasingleview(Swift)【发布时间】:2017-08-1321:12:48【问题描述】:当我使用多个UIScrollViews时,我对如何正确滚动到图像有点困惑我有pinchScrollView1pinchScrollView2图... 查看详情
Android 如何使用 BitmapFactory 选项缩放图像
...题描述】:我想使用BitmapFactory.decodeFile(path,options)对SD卡中的图像进行解码。我还希望将大于2048x2048像素的图像缩小到最多2048x2048(保持比例)。我可以在获 查看详情
iOS Xcode 使背景图像缩放以填充
...置相同的背景。这是我的背景:这就是我的ImageView在Xcode中的样子:问题是为什么当我在模拟器(iPhone4s)上运行它时它不能扩展填充?对这个问题有什么想法吗?【问题讨论】:您必 查看详情
首页英雄背景图像在移动设备上未正确缩放
】首页英雄背景图像在移动设备上未正确缩放【英文标题】:FrontpageherobackgroundimageimproperlyscaledonMobile【发布时间】:2018-04-2004:15:07【问题描述】:在我的wordpress网站上,首页英雄的背景在移动设备(iOS和Android)上缩放不当。它... 查看详情
我们是不是应该在将图像设置为 UITableView 中的 imageView 之前缩放图像
】我们是不是应该在将图像设置为UITableView中的imageView之前缩放图像【英文标题】:ShouldwescaleimagebeforesettingittoaimageViewinUITableView我们是否应该在将图像设置为UITableView中的imageView之前缩放图像【发布时间】:2016-03-0706:34:07【问题... 查看详情
在iOS中使用动画将图像缩放到特定的矩形?
】在iOS中使用动画将图像缩放到特定的矩形?【英文标题】:ZoomanimagetoaparticularrectwithanimationiniOS?【发布时间】:2016-07-0706:55:01【问题描述】:我的应用程序中有一个UIImageView。我在UIScrollView中添加了它。现在当我点击图像时,... 查看详情
如何在ios模拟器中缩放图像?
...方案1】:按键盘和鼠标触控板上的“选项”键。模拟器中的鼠标指针将变为两个圆圈,然后通过在触控板上拖动,您可以放大和缩小图像。它就像原生i 查看详情
在 ios 的滚动视图中绘制缩放的图像视图?
】在ios的滚动视图中绘制缩放的图像视图?【英文标题】:Drawingonzoomedimageviewinscrollviewinios?【发布时间】:2013-09-2112:26:04【问题描述】:嗨朋友们,我正在创建ipad应用程序,我在UIScrollView中使用UIImageView。因此,当我缩放图像并... 查看详情