uiimageview 的移动、缩放和旋转类

发布于 2024-07-24 20:30:35 字数 175 浏览 4 评论 0原文

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

有没有人有任何例子,或者链接到已经这样做的课程? 或者,如果您有自己编写的课程,那也会很有帮助。

预先非常感谢。

I'm creating a subclass of UIImageView that detects touches, and will move, rotate and scale the image based on the touches. However, I really feel like I'm reinventing the wheel here, and it's driving me nuts. Shouldn't this already exist somewhere?

Does anyone have any examples, or links to a class that is already doing this? Or if you have a class you've written, that'd be helpful too.

Thanks a lot in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

合约呢 2024-07-31 20:30:35

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

希望这对某人有用。

对于任何感兴趣的人,这里是 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;

}  

I figured it out... I answered my own question.

Hopefully this is useful to someone.

For anyone who is interested, here is the implementation for a UIImageView subclass, that you can use to move, scale, and rotate an image. It works pretty well for me.

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

}  
梦晓ヶ微光ヅ倾城 2024-07-31 20:30:35

您还可以查看 iPhone 开发食谱 (oreilly) 中 Erica Sadun 的示例。 这是关于手势和触摸的第 8 章。

http:// github.com/erica/iphone-3.0-cookbook-/tree/master/C08-Gestures/14-Resize%20And%20Rotate/

You might also look at Erica Sadun's example from the iphone dev cookbook (oreilly). It's in chapter 8 on gestures and touches.

http://github.com/erica/iphone-3.0-cookbook-/tree/master/C08-Gestures/14-Resize%20And%20Rotate/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文