图像未放大时如何停止移动图像?

     2023-03-11     233

关键词:

【中文标题】图像未放大时如何停止移动图像?【英文标题】:how to stop moving image when image is not zoomed in? 【发布时间】:2015-11-02 11:14:36 【问题描述】:

我有一个UIImageView 我正在执行缩放缩放和全缩放移动图像。我可以移动图像但问题是即使图像没有放大它仍然会在屏幕上移动。请告诉我如何预防?

这是代码

#define MINIMUM_SCALE 0.5
#define MAXIMUM_SCALE 6.0
@property CGPoint translation;


- (void)pan:(UIPanGestureRecognizer *)gesture 
    static CGPoint currentTranslation;
    static CGFloat currentScale = 0;
    if (gesture.state == UIGestureRecognizerStateBegan) 
        currentTranslation = _translation;
        currentScale = self.view.frame.size.width / self.view.bounds.size.width;
    
    if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateChanged) 

        CGPoint translation = [gesture translationInView:self.view];

        _translation.x = translation.x + currentTranslation.x;
        _translation.y = translation.y + currentTranslation.y;
        CGAffineTransform transform1 = CGAffineTransformMakeTranslation(_translation.x , _translation.y);
        CGAffineTransform transform2 = CGAffineTransformMakeScale(currentScale, currentScale);
        CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
        self.view.transform = transform;
    



- (void)pinch:(UIPinchGestureRecognizer *)gesture 
    if (gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateChanged) 
//        NSLog(@"gesture.scale = %f", gesture.scale);

        CGFloat currentScale = self.view.frame.size.width / self.view.bounds.size.width;
        CGFloat newScale = currentScale * gesture.scale;

        if (newScale < MINIMUM_SCALE) 
            newScale = MINIMUM_SCALE;
        
        if (newScale > MAXIMUM_SCALE) 
            newScale = MAXIMUM_SCALE;
        

        CGAffineTransform transform1 = CGAffineTransformMakeTranslation(_translation.x, _translation.y);
        CGAffineTransform transform2 = CGAffineTransformMakeScale(newScale, newScale);
        CGAffineTransform transform = CGAffineTransformConcat(transform1, transform2);
        self.view.transform = transform;
        gesture.scale = 1;
    

**PS:**ImageView 应该只在图像缩放时移动,否则它不应该是可移动的。

【问题讨论】:

完成 - 我猜你欠我两个赏金好友;) 【参考方案1】:

我建议一种不同的方法:

创建一个UIScrollViewUIImageView 添加到scrollView 的contentView 并允许在scrollView 上滚动和缩放。

为防止在未缩放时滚动,请将setScrollEnabled 设置为NO 并在zoomLevel 达到自定义阈值时在scrollViewDidZoom 中启用它。

等等。像这样应该可以使您尝试用更少的代码完成的工作变得更加容易。

- (void)viewDidLoad:(BOOL)animated 
    [super viewDidLoad:animated];
    self.scrollView.scrollEnabled = NO;
    self.scrollView.minimumZoomScale = 1;


- (void)scrollViewDidZoom:(UIScrollView *)scrollView 
    if (scrollView.zoomLevel > 1) 
        scrollView.scrollEnabled = YES;
     else 
        scrollView.scrollEnabled = NO;
    

【讨论】:

如果它解决了你的问题,你能把它标记为正确答案吗?【参考方案2】:

我重新发布了我在您关于该主题的第一个问题 (Pinch zoom shifting image to most left corner on iPad in iOS?) 中给您的代码,因为我认为您执行此操作的方式无法按预期工作(据我测试)。

捏缩放应该在手指中间缩放,而不是在图像中间。如果你真的想在中心应用缩放,我添加了一个布尔属性 zoomOnCenter。

边界检查在 translateBy 方法中完成。

这里是 xCode 测试项目的链接:https://drive.google.com/file/d/0B88aMtNA0z2aWVBIbGZGdVhpdWM/view?usp=sharing

界面

@interface PinchViewController : UIViewController

@property(nonatomic,strong) IBOutlet UIView* contentView;
@property(nonatomic,assign) BOOL zoomOnCenter;

@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.zoomOnCenter ? CGPointZero : [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];


- (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];



-(void)translateBy:(CGPoint)delta

    CGSize contentSize = self.contentView.bounds.size;
    CGSize viewSize = self.view.bounds.size;
    CGSize  scaledViewSize = viewSize;
    scaledViewSize.width/=scale;
    scaledViewSize.height/=scale;
    CGPoint maxTranslation = CGPointMake( (contentSize.width-scaledViewSize.width) / 2.0f , (contentSize.height-scaledViewSize.height) / 2.0f );

    if ( contentSize.width*scale < viewSize.width ) 
        delta.x=0;
        translation.x = 0;
     else 
        translation.x+=delta.x;
        if ( translation.x < - maxTranslation.x ) 
            translation.x = - maxTranslation.x;
        
        else if ( translation.x > maxTranslation.x ) 
            translation.x = maxTranslation.x;
        
    

    if ( contentSize.height*scale < viewSize.height ) 
        delta.y=0; translation.y = 0;
     else 
        translation.y+=delta.y;
        if ( translation.y < - maxTranslation.y ) 
            translation.y = - maxTranslation.y;
        
        else if ( translation.y > maxTranslation.y ) 
            translation.y = maxTranslation.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

【讨论】:

【参考方案3】:

您可以检查 imageView 的框架,如果相同,则表示 imageview 尚未缩放因此,如果 imageview 的框架相同,您可以禁用平移手势......如果框架已更改,那么您可以启用平移手势.....

【讨论】:

UIScrollView 放大时将图像移动到左上角

】UIScrollView放大时将图像移动到左上角【英文标题】:UIScrollViewmovesimagetothetopleftcornerwhenzoomingin【发布时间】:2012-02-0514:52:03【问题描述】:看看这个问题:PreventUIScrollViewfrommovingcontentstotop-left,我遇到了确切的问题。我正在使... 查看详情

不幸的是,当在android中通过横向模式捕获图像时,图像未旋转已停止?

】不幸的是,当在android中通过横向模式捕获图像时,图像未旋转已停止?【英文标题】:Unfortunately,Imagenotrotedhasstopped,whenimagecapturevialandscapemodeinandroid?【发布时间】:2014-07-1116:56:55【问题描述】:我的代码是:MainActivity.java:publ... 查看详情

Maphilight() 在放大/缩小图像地图后停止正常工作

】Maphilight()在放大/缩小图像地图后停止正常工作【英文标题】:Maphilight()stopsworkingcorrectlyafterzoomin/outofimagemap【发布时间】:2018-11-0207:28:52【问题描述】:我有一个包含300-400个多边形区域的图像映射,在“onclick”事件中应该突... 查看详情

按下“停止”按钮时如何停止动画并冻结图像

】按下“停止”按钮时如何停止动画并冻结图像【英文标题】:Howtostoptheanimationandfreezetheimagewhenpressingthe`Stop`button【发布时间】:2019-07-1812:37:53【问题描述】:我创建了工作行星齿轮的布局。当您点击Stop按钮时,齿轮旋转动画应... 查看详情

如何放大按钮图像?

】如何放大按钮图像?【英文标题】:HowdoIenlargeabuttonsimage?【发布时间】:2015-12-2212:15:23【问题描述】:我希望这个图像(它是一个按钮)更大一点,通过拖动角我使按钮变大,但图像本身不会缩放。设置宽度和高度的约束也无... 查看详情

swift - 如何在多个页面中快速缩放图像

】swift-如何在多个页面中快速缩放图像【英文标题】:Howtozoomimageswhentheyareinmultiplepagesinswift【发布时间】:2016-07-3005:07:10【问题描述】:我有一个主滚动视图,在该主滚动视图中我有一个具有UIImageView的子滚动视图。我在正确缩... 查看详情

如何防止 WPF 剪切我的图像?

】如何防止WPF剪切我的图像?【英文标题】:HowcanIpreventWPFfromclippingmyimage?【发布时间】:2013-02-1202:14:15【问题描述】:我正在编写一个WPF应用程序,它显示最初居中的图像。用户可以放大/缩小和移动图像,使用ScaleTransform和Trans... 查看详情

如何在android中结合覆盖位图和捕获的图像?

】如何在android中结合覆盖位图和捕获的图像?【英文标题】:Howtocombineoverlaybitmapandcapturedimageinandroid?【发布时间】:2016-01-2522:11:28【问题描述】:我要求我需要创建一个自定义相机并允许用户在捕获图像时放置徽标。徽标可以... 查看详情

当对象移动到某个帧时如何停止 UIPanGestureRecognizer

...ame【发布时间】:2011-11-1011:25:55【问题描述】:我有一个图像类型的对象,我正在使用UIPanGestureRecognizer移动它,当对象到达某个帧时,我需要停止识别UIPanGestureRecognizer。U 查看详情

背景图像未切换到移动设备上的备用图像

】背景图像未切换到移动设备上的备用图像【英文标题】:Backgroundimagesnotswitchingtoalternateimagesonmobiledevice【发布时间】:2017-04-0722:54:54【问题描述】:我一直在开发我的投资组合网站(在www.imkev.in上查看),但我在使用移动版本... 查看详情

点击时如何停止标签栏项目图像的背景宽度变化?

】点击时如何停止标签栏项目图像的背景宽度变化?【英文标题】:HowtostopTabBarItemimages\'backgroundwidthchangingwhentapped?【发布时间】:2018-12-0122:42:16【问题描述】:当我单击标签栏项目图像时,它们的背景(或其他任何内容)宽度... 查看详情

图像缩放时在 UIScrollView 中停止滚动图像

】图像缩放时在UIScrollView中停止滚动图像【英文标题】:StopscrollimageinUIScrollViewwhenimagezoom【发布时间】:2012-09-2513:49:18【问题描述】:我在UIScrollView中有一个图像,我希望用户不能在缩放时滚动图像,我阅读了UIScrollView的许多属... 查看详情

Soundpool 停止问题?

...:33:47【问题描述】:Soundpool停止功能不起作用。我有很多图像,当我需要播放与图像相关的声音时,我有很多图像,我尝试使用soundpool对象音乐播放得很好。但是如果我快速将1个图像移动到第1个图像的2个图像,我的问题是滚动... 查看详情

放大 S3 图像 - 404 未找到

】放大S3图像-404未找到【英文标题】:AmplifyS3Image-404NotFound【发布时间】:2021-08-0205:18:23【问题描述】:我正在尝试访问已上传到我的S3存储桶的图像。我使用AmplifyCLI(amplifyaddstorage)创建了我的存储桶,并授予对我所有cognito组的... 查看详情

使用溢出时如何移动图像的 POV:隐藏?

】使用溢出时如何移动图像的POV:隐藏?【英文标题】:HowtomovethePOVofanimagewhenusingoverflow:hidden?【发布时间】:2016-08-0818:33:37【问题描述】:所以经过长时间的搜索,我终于找到了如何使用overflow:hidden;在不扭曲/压缩图像的情况下... 查看详情

放大iphone sdk时如何调整滚动视图的子视图

...发布时间】:2012-06-2616:46:15【问题描述】:我有一个带有图像的滚动视图。用户可以触摸滚动视图并在其上放置矩形视图。我希望当用户捏缩放滚动视图时,子子视图应根据比例自动调整大小。用户还应该能够通过触摸事件移动... 查看详情

上传所有移动设备时如何调整图像大小?

】上传所有移动设备时如何调整图像大小?【英文标题】:Howtoresizeimagewhenuploadforallmobiledevices?【发布时间】:2019-06-0805:43:36【问题描述】:我只是构建了一个示例反应原生项目,当我上传到服务器的原始图像不适合其他移动屏... 查看详情

swinger.js 网站图像在光标移动时交换,如何制作图像标题?

】swinger.js网站图像在光标移动时交换,如何制作图像标题?【英文标题】:swinger.jswebsiteimageswaponcursormove,howtomakeimagecaption?【发布时间】:2021-07-1019:15:12【问题描述】:在过去的一周里,我一直在尝试创建一个100vh和vw的网站,当... 查看详情