0

您好我有一个要求,我需要通过拖动图像视图的右上角来调整和旋转图像视图。

我可以通过拖动它的角成功地调整图像视图的大小,但是当我尝试用一​​根手指为图像视图实现调整大小和旋转功能时失败了。

任何人都可以指导我实现我的要求。

我添加了下面的代码,用于从角边缘调整图像视图的大小。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
touchStart = [[touches anyObject] locationInView:self];
isResizingLR = (self.bounds.size.width - touchStart.x < kResizeThumbSize && self.bounds.size.height - touchStart.y < kResizeThumbSize);
isResizingUL = (touchStart.x <kResizeThumbSize && touchStart.y <kResizeThumbSize);
isResizingUR = (self.bounds.size.width-touchStart.x < kResizeThumbSize && touchStart.y<kResizeThumbSize);
isResizingLL = (touchStart.x <kResizeThumbSize && self.bounds.size.height -touchStart.y <kResizeThumbSize);

}

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

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

if (isResizingLR) {
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
}  
if (isResizingUL) {
    self.frame = CGRectMake(self.frame.origin.x + deltaWidth, self.frame.origin.y + deltaHeight, self.frame.size.width - deltaWidth, self.frame.size.height - deltaHeight);
} 
if (isResizingUR) {
    self.frame = CGRectMake(self.frame.origin.x ,self.frame.origin.y + deltaHeight,  self.frame.size.width + deltaWidth, self.frame.size.height - deltaHeight);      
} 
if (isResizingLL) {
    self.frame = CGRectMake(self.frame.origin.x + deltaWidth ,self.frame.origin.y ,  self.frame.size.width - deltaWidth, self.frame.size.height + deltaHeight);   
}

if (!isResizingUL && !isResizingLR && !isResizingUR && !isResizingLL) {
    self.center = CGPointMake(self.center.x + touchPoint.x - touchStart.x,self.center.y + touchPoint.y - touchStart.y);
}

}

4

2 回答 2

1

嗨,通过UIImageView代码示例调整大小

试试这个 ...

我希望它会帮助你...

试试这个旋转...

self.imgViewForBend.layer.anchorPoint = CGPointMake(0.0,1.0);
self.imgViewForBend.layer.position = CGPointMake(200,300.0);
CGAffineTransform cgaRotateHr = CGAffineTransformMakeRotation(-(3.141/4));
[self.imgViewForBend setTransform:cgaRotateHr];
于 2012-09-24T11:37:54.023 回答
0

没有任何代码我会想到这样的事情......

当触摸开始时,存储您正在编辑的图像的起点和中心点。(所有点都相对于图像的 superView)。

然后比例是相对于触摸到中心点的距离(使用毕达哥拉斯)。

此外,您将知道从中心点到原始触摸线的角度(使用三角法)。您可以根据起始角度和当前角度之间的差异来更改图像的角度。

我还将从触摸移动和触摸开始功能中取出逻辑。制作一个 calculateRotation 函数和一个 calculateScale 函数来提供帮助。

不过暂时无法编码。

于 2012-09-24T08:11:15.093 回答