gpt4 book ai didi

iphone - uiimageview 的移动、缩放和旋转类

转载 作者:行者123 更新时间:2023-12-03 20:21:43 27 4
gpt4 key购买 nike

我正在创建 UIImageView 的子类,它可以检测触摸,并根据触摸移动、旋转和缩放图像。然而,我真的觉得我正在重新发明轮子,这让我发疯。这不应该已经存在于某个地方吗?

有人有任何示例或已经在执行此操作的类的链接吗?或者,如果您有自己编写的类(class),那也会很有帮助。

提前非常感谢。

最佳答案

我想通了...我回答了我自己的问题。

希望这对某人有用。

对于任何感兴趣的人,这里是 UIImageView 子类的实现,您可以使用它来移动、缩放和旋转图像。它对我来说效果很好。

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

if( [touches count] == 1 ) {

float difx = [[touches anyObject] locationInView:self].x - [[touches anyObject] previousLocationInView:self].x;

float dify = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;



CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify);

self.transform = newTransform1;

} else if( [touches count] == 2 ) {

int prevmidx = ([[[touches allObjects] objectAtIndex:0] previousLocationInView:self].x + [[[touches allObjects] objectAtIndex:1] previousLocationInView:self].x) / 2;

int prevmidy = ([[[touches allObjects] objectAtIndex:0] previousLocationInView:self].y + [[[touches allObjects] objectAtIndex:1] previousLocationInView:self].y) / 2;

int curmidx = ([[[touches allObjects] objectAtIndex:0] locationInView:self].x + [[[touches allObjects] objectAtIndex:1] locationInView:self].x) / 2;

int curmidy = ([[[touches allObjects] objectAtIndex:0] locationInView:self].y + [[[touches allObjects] objectAtIndex:1] locationInView:self].y) / 2;

int difx = curmidx - prevmidx;

int dify = curmidy - prevmidy;



CGPoint prevPoint1 = [[[touches allObjects] objectAtIndex:0] previousLocationInView:self];

CGPoint prevPoint2 = [[[touches allObjects] objectAtIndex:1] previousLocationInView:self];

CGPoint curPoint1 = [[[touches allObjects] objectAtIndex:0] locationInView:self];

CGPoint curPoint2 = [[[touches allObjects] objectAtIndex:1] locationInView:self];

float prevDistance = [self distanceBetweenPoint1:prevPoint1 andPoint2:prevPoint2];

float newDistance = [self distanceBetweenPoint1:curPoint1 andPoint2:curPoint2];

float sizeDifference = (newDistance / prevDistance);



CGAffineTransform newTransform1 = CGAffineTransformTranslate(self.transform, difx, dify);

self.transform = newTransform1;



CGAffineTransform newTransform2 = CGAffineTransformScale(self.transform, sizeDifference, sizeDifference);

self.transform = newTransform2;





float prevAngle = [self angleBetweenPoint1:prevPoint1 andPoint2:prevPoint2];

float curAngle = [self angleBetweenPoint1:curPoint1 andPoint2:curPoint2];

float angleDifference = curAngle - prevAngle;



CGAffineTransform newTransform3 = CGAffineTransformRotate(self.transform, angleDifference);

self.transform = newTransform3;

}

}



- (NSInteger)distanceBetweenPoint1:(CGPoint)point1 andPoint2:(CGPoint)point2 {

CGFloat deltaX = fabsf(point1.x - point2.x);

CGFloat deltaY = fabsf(point1.y - point2.y);

CGFloat distance = sqrt((deltaY*deltaY)+(deltaX*deltaX));

return distance;

}



- (CGFloat)angleBetweenPoint1:(CGPoint)point1 andPoint2:(CGPoint)point2

{

CGFloat deltaY = point1.y - point2.y;

CGFloat deltaX = point1.x - point2.x;

CGFloat angle = atan2(deltaY, deltaX);

return angle;

}

关于iphone - uiimageview 的移动、缩放和旋转类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/999616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com