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

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

问题描述

我正在开发一个应用程序,它具有通过拖动其右下角按钮来调整和旋转图像视图的功能.

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

我看到一个应用程序具有这样的功能:如果我们将右下角按钮对角拖动,则图像视图大小已调整大小,或者如果我们将按钮向左或右侧方向拖动,则图像视图已按方向旋转.我希望在我的应用中实现此功能

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

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

I am struggling to implement single finger rotation as well as resizing the 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天全站免登陆