【问题标题】:Move UIView into a UIScrollView doesn't work after first zoom将 UIView 移动到 UIScrollView 在第一次缩放后不起作用
【发布时间】:2012-11-06 19:21:56
【问题描述】:

我正在尝试使用 CATiledLayer 和 UIScrollView 显示一个大而高质量的可缩放图像。我的代码基于iOS Large image downsizing sample code

我的 UIScrollView 的层次结构是这样的:

  • UIScrollView
    • UIImageView : 代表大图的第一个缩略图
    • 旧的 TiledView
    • 平铺视图

我希望滚动视图中的图像可以移动。

我尝试了我在此 StackOverflow topic 上找到的内容,它有效。但是在第一次放大图像后,就无法移动图像了。不知道为什么?

这是我的代码:

-(id)initWithFrame:(CGRect)frame image:(UIImage*)_image {
    if((self = [super initWithFrame:frame])) {      
    // Set up the UIScrollView
        // Piece of code

        self.canCancelContentTouches = NO;

        UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture setMinimumNumberOfTouches:1];
        [panGesture setMaximumNumberOfTouches:1];
        [panGesture setDelegate:self];
        [frontTiledView addGestureRecognizer:panGesture];
        frontTiledView.exclusiveTouch = YES;

        UIPanGestureRecognizer* panGesture2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture2 setMinimumNumberOfTouches:1];
        [panGesture2 setMaximumNumberOfTouches:1];
        [panGesture2 setDelegate:self];
        [backgroundImageView addGestureRecognizer:panGesture2];
        backgroundImageView.exclusiveTouch = YES;

        UIPanGestureRecognizer* panGesture3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panGesture3 setMinimumNumberOfTouches:1];
        [panGesture3 setMaximumNumberOfTouches:1];
        [panGesture3 setDelegate:self];
        [backTiledView addGestureRecognizer:panGesture3];
        backTiledView.exclusiveTouch = YES;
    }
    return self;
}

- (void)move:(UIGestureRecognizer*)sender {
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:frontTiledView];

    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        positionDeplacement = frontTiledView.center;
    }

    translatedPoint = CGPointMake(positionDeplacement.x+translatedPoint.x, positionDeplacement.y+translatedPoint.y);
    frontTiledView.center = translatedPoint;
    backTiledView.center = translatedPoint;
    backgroundImageView.center = translatedPoint;
}

谢谢。

【问题讨论】:

  • 这有点令人困惑。您似乎想要同时且平等地移动三个视图。我已经加载了 LargeImage 示例,并且可以毫无问题地平移和缩放该图像。你想做什么不一样?
  • LargeImage 示例滚动视图中有 3 个视图,这就是为什么我想同时且均等地移动它们。您在哪个子视图上应用平移手势?
  • 我无法使用您的代码重现您所描述的内容。我不确定你为什么要在UIScrollView 中使用UIPanGestureRecognizer,如果你正在这样做的话。滚动视图有自己的平移委托方法。
  • 我想使用 UIPanGestureRecognizer 使图像进入滚动视图可移动。在 ImageScrollView 文件中,我们有 3 个对象:frontTiledView、backTiledView 和 backgroundImageView。我试图将手势应用于这 3 个视图以获得我想要的。也许这不是这样做的好方法。我可以理解吗? :/如果你做到了,你能把你的代码发给我吗?我会成为世界上最幸福的人^^
  • 好的,我想我明白你在问什么。您想在滚动视图中移动图像,而不仅仅是使用滚动视图滚动图像。我不知道该怎么做!

标签: ios uiscrollview uiimageview zooming catiledlayer


【解决方案1】:

我找到了解决问题的方法。我只需要在 scrollViewDidEndZooming 委托方法中再次添加手势,它就像一个魅力:

这是我的代码:

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
    // set the new scale factor for the TiledImageView
    imageScale *=scale;
    CGRect imageRect = CGRectMake(0.0f,0.0f,CGImageGetWidth(image.CGImage) * imageScale,CGImageGetHeight(image.CGImage) * imageScale);

    // Create a new TiledImageView based on new frame and scaling.
    frontTiledView = [[TiledImageView alloc] initWithFrame:imageRect image:image scale:imageScale]; 
    [self addSubview:frontTiledView];
    [frontTiledView release];

    if (imageRect.size.width < 320 || imageRect.size.height < 460)
        [self initGestures];
}

- (void)initGestures {
    self.canCancelContentTouches = NO;
    frontTiledView.exclusiveTouch = YES;
    backgroundImageView.exclusiveTouch = YES;
    backTiledView.exclusiveTouch = YES;

    UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture setMinimumNumberOfTouches:1];
    [panGesture setMaximumNumberOfTouches:1];
    [panGesture setDelegate:self];
    [frontTiledView addGestureRecognizer:panGesture];

    UIPanGestureRecognizer* panGesture2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture2 setMinimumNumberOfTouches:1];
    [panGesture2 setMaximumNumberOfTouches:1];
    [panGesture2 setDelegate:self];
    [backgroundImageView addGestureRecognizer:panGesture2];

    UIPanGestureRecognizer* panGesture3 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panGesture3 setMinimumNumberOfTouches:1];
    [panGesture3 setMaximumNumberOfTouches:1];
    [panGesture3 setDelegate:self];
    [backTiledView addGestureRecognizer:panGesture3];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 2017-02-13
    • 2011-07-03
    • 2019-07-14
    • 2017-08-16
    相关资源
    最近更新 更多