如何用单指旋转图像视图并调整其大小

tha*_*rai 4 iphone rotation uiimageview image-resizing ios

我正在开发一个应用程序,其功能是通过拖动其右下角按钮来调整和旋转imageview.

我看到一个应用程序的功能,如果我们拖动右下角按钮对角线imageview大小已调整大小,否则如果我们拖动按钮左侧或右侧方向imageview已按方向旋转.我希望在我的应用中实现此功能

我正在努力实现单指旋转以及调整imageview的大小.

我可以通过拖动它的右下角按钮成功实现调整imageview的大小.但我没有足够的知识来向图像视图添加旋转

请以正确的方式指导我.

我已添加以下代码,通过拖动其右上角来调整图像视图的大小.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];

    touchStart = [[touches anyObject] locationInView:imageView];
    isResizingLR = (containerVw.bounds.size.width - touchStart.x < kResizeThumbSize && containerVw.bounds.size.height - touchStart.y < kResizeThumbSize);

}



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

UITouch *touch = [[event allTouches] anyObject];


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


    if (isResizingLR) {
        containerVw.frame = CGRectMake(containerVw.frame.origin.x, containerVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth); 
        imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);                      
        dragIm.frame = CGRectMake(containerVw.frame.size.width-10, containerVw.frame.size.height-10,20,20);


    if (!isResizingLR) {
        containerVw.center = CGPointMake(containerVw.center.x + touchPoint.x touchStart.x,containerVw.center.y + touchPoint.y - touchStart.y);
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Kim*_*Kim 10

我遇到了与你相同的障碍,所以我开发了自己的模块ZDStickerView.这将是一个很好的参考.

首先,确保您的视图的autoresizingMask应该是灵活的.

    autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
Run Code Online (Sandbox Code Playgroud)

否则调整大小将无法正常工作.

其次,我建议你使用"CGAffineTransformMakeRotation"和"atan2"函数来解决旋转问题,如下所示:

    float ang = atan2([recognizer locationInView:self.superview].y - self.center.y,
                      [recognizer locationInView:self.superview].x - self.center.x);
    float angleDiff = deltaAngle - ang;
    self.transform = CGAffineTransformMakeRotation(-angleDiff);
Run Code Online (Sandbox Code Playgroud)

第三,一定要使用相对坐标,如下所示:

    self.transform = CGAffineTransformMakeRotation(-angleDiff);
Run Code Online (Sandbox Code Playgroud)