【问题标题】:How to restrict the image translation outside ipad screen如何限制 ipad 屏幕外的图像翻译
【发布时间】:2011-11-02 15:53:58
【问题描述】:

我目前正在使用UIPanGestureRecognizer 在我的 ipad 屏幕中翻译图像,但问题是图像超出了屏幕。 那么我怎样才能限制 ipad 屏幕外的图像移动。

- (void)translate:(UIPanGestureRecognizer *)gesture {
    CGPoint myTranslation = [gesture translationInView:self];
    self.transform = CGAffineTransformTranslate(self.transform, myTranslation.x, myTranslation.y);
    [gesture setTranslation:CGPointZero inView:self];
}

【问题讨论】:

    标签: iphone ipad uiimage restrict uipangesturerecognizer


    【解决方案1】:

    您应该检查边界并确定图像的新位置是否有效。然后你可以根据它应用或不应用变换。

    【讨论】:

    • 你能发布示例 sn-p 我怎么能这样做,因为我是 ios 新手
    • 我在另一个答案中发布了相关代码sn-p。如果没有看到所有代码,很难说更多。希望对您有所帮助。
    【解决方案2】:

    此代码来自我的一个项目 - SSPhotoCropperViewController。在这里,我让用户在滚动视图中移动图像,但我不希望他们将图像移动到滚动视图的范围之外。我处理图像视图的 UIControlEventTouchDragInside 事件,确定新位置并检查新位置是否有效,然后决定是否移动图像。这是代码sn-p。如果没有看到所有代码,很难说更多。您可以在这里使用这个想法并将其应用到您的案例中,这应该不会太难。希望这会有所帮助。

    - (BOOL) isRectanglePositionValid:(CGPoint)pos
    {
        CGRect innerRect = CGRectMake((pos.x + 15), (pos.y + 15), 150, 150);
        return CGRectContainsRect(self.scrollView.frame, innerRect);
    }
    
    - (IBAction) imageMoved:(id)sender withEvent:(UIEvent *)event
    {
        CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    
        CGPoint prev = _lastTouchDownPoint;
        _lastTouchDownPoint = point;
        CGFloat diffX = point.x - prev.x;
        CGFloat diffY = point.y - prev.y;
    
        UIControl *button = sender;
        CGRect newFrame = button.frame;
        newFrame.origin.x += diffX;
        newFrame.origin.y += diffY;
        if ([self isRectanglePositionValid:newFrame.origin]) {
            button.frame = newFrame;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多