如何用单指旋转图像视图并调整其大小 [英] How to rotate and resize the image view with single finger

查看:181
本文介绍了如何用单指旋转图像视图并调整其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款应用程序,其功能是通过拖动其右下角按钮来调整和旋转图像视图。

I am developing an app which has feature that resizing and rotating the imageview by dragging its bottom right corner button.

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

I saw one app which has feature that if we drag the bottom right corner button diagonally imageview size had resized or else if we drag the button left or right side direction imageview had rotated as per direction. I wish to implement this feature in my app

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

I am struggling to implement single finger rotation as well as resizing the imageview.

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

I could successfully implement resizing the imageview by dragging its bottom right corner button. But I do not have enough knowledge to add rotation to the image view

请以正确的方式指导我。

Please guide me in right way.

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

I have added the code below for resizing the image view by dragging its right corner.

- (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);
    }
}

推荐答案

我遇到了同样的障碍和你一样,我开发了自己的模块 ZDStickerView 。这将是一个不错的参考。

I had hitted the same obstacle as yours, so I developed my own modules ZDStickerView. It would be nice reference.

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

First of all, be sure your view's autoresizingMask should be flexible.

    autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

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

otherwise resizing wouldn't work properly.

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

Second, I recommends you to use "CGAffineTransformMakeRotation" and "atan2" functions to solve the rotation problem, like this:

    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);

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

Third, be sure to use relative coordinate, like this:

    self.transform = CGAffineTransformMakeRotation(-angleDiff);

这篇关于如何用单指旋转图像视图并调整其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆