xcode调整大小和位置坐标

时间:2014-12-11 18:35:16

标签: ios xcode resize touch coordinates

我希望我的代码(ipad app)能够通过触摸来处理图像的大小调整。 我对xcode如何处理点坐标感到困惑。 我编写了我的代码,考虑到框架的原点(frame.origin)位于左上方(我在某处读取),触摸位置是手指触摸屏幕的坐标,左侧是正方向( x)和向上(y)。如果我错在那里,那将解释为什么我的代码不能正常工作。 这是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGFloat kResizeThumbSize = 45.0f;
    touchStart = [[touches anyObject] locationInView:self.view];
    CGFloat xO = _ImageView.frame.origin.x;
    CGFloat yO = _ImageView.frame.origin.y;
    CGFloat width = _ImageView.frame.size.width;
    CGFloat height = _ImageView.frame.size.height;
    isResizingUD = (xO - kResizeThumbSize < touchStart.x < xO + width + kResizeThumbSize &&
                (yO - kResizeThumbSize < touchStart.y < yO + kResizeThumbSize ||
                yO - height - kResizeThumbSize < touchStart.y < yO - height + kResizeThumbSize) );
    isResizingLR = (yO + kResizeThumbSize < touchStart.x < xO + width - kResizeThumbSize &&
                (xO - kResizeThumbSize < touchStart.x < xO + kResizeThumbSize ||
                 xO - width - kResizeThumbSize < touchStart.x < xO - width + kResizeThumbSize));
}

//moving the image
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    CGPoint previous = [[touches anyObject] previousLocationInView:self.view];

    CGFloat deltaWidth = touchPoint.x - previous.x;
    CGFloat deltaHeight = touchPoint.y - previous.y;

    // get the frame values so we can calculate changes below
    CGFloat x = _ImageView.frame.origin.x;
    CGFloat y = _ImageView.frame.origin.y;
    CGFloat width = _ImageView.frame.size.width;
    CGFloat height = _ImageView.frame.size.height;

    if (isResizingLR) {
        _ImageView.frame = CGRectMake(x, y, width+deltaWidth, height);
    } else if (isResizingUD) {
        _ImageView.frame = CGRectMake(x, y, width, height+deltaHeight);
    } else {
        // not dragging from a corner -- move the view
        _ImageView.center = CGPointMake(self.view.center.x + touchPoint.x - touchStart.x,
                                    self.view.center.y + touchPoint.y - touchStart.y);
    }

}

我希望只有当用户在kResizeThumbSize中触摸图像的底部或顶部时才能在上/下方向调整图像(实际上,我不确定它是否表示单位)以及如果用户在kResizeThumbSize内触摸图像的左边框或右边框,则为左/右方向。不幸的是,发生的事情是图像只能在左/右方向上调整大小,无论我放在哪里,它都会调整大小。 非常欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

固定!

对于需要帮助的人,我发布了我的代码。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGFloat kResizeThumbSize = 10.0f;
    touchStart = [[touches anyObject] locationInView:self.view];
    CGFloat xO = _ImageView.frame.origin.x;
    CGFloat yO = _ImageView.frame.origin.y;
    CGFloat width = _ImageView.frame.size.width;
    CGFloat height = _ImageView.frame.size.height;
    isResizingUD = (xO - kResizeThumbSize < touchStart.x &&
                touchStart.x < xO + width + kResizeThumbSize &&
                yO + height - kResizeThumbSize < touchStart.y);
    isResizingLR = (yO + kResizeThumbSize < touchStart.y &&
                touchStart.y< yO + height - kResizeThumbSize &&
                xO + width - kResizeThumbSize < touchStart.x);
    isMoving = (xO+kResizeThumbSize< touchStart.x &&
            touchStart.x < xO + width - kResizeThumbSize  &&
            yO + kResizeThumbSize < touchStart.y &&
            touchStart.y< yO + height - kResizeThumbSize);

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{    
    CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
    CGPoint previous = [[touches anyObject] previousLocationInView:self.view];

    CGFloat deltaWidth = touchPoint.x - previous.x;
    CGFloat deltaHeight = touchPoint.y - previous.y;

    // get the frame values so we can calculate changes below
    CGFloat x = _ImageView.frame.origin.x;
    CGFloat y = _ImageView.frame.origin.y;
    CGFloat width = _ImageView.frame.size.width;
    CGFloat height = _ImageView.frame.size.height;

    if (isResizingUD) {
        _ImageView.frame = CGRectMake(x, y, width, height+deltaHeight);
    } else if (isResizingLR) {
        _ImageView.frame = CGRectMake(x, y, width+deltaWidth, height);
    } else if (isMoving) {
        // not dragging from a corner -- move the view
        _ImageView.frame = CGRectMake(x+deltaWidth, y+deltaHeight, width, height);
    }

}
相关问题