iphone - 如何在iPhone的速度计中旋转图像

标签 iphone objective-c ios uiimage cgaffinetransform

以下是在触摸事件上旋转箭头(uiimage)的代码。在这种情况下,我减去了先前和当前两个触摸的位置,但是问题是箭头(uiimage)有时会沿触摸的相反方向移动。

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];        
    NSLog(@"%@",[touch view]);   
    if ([touch view] == ImgViewArrowImage) 
    {
    CGPoint currentLocation = [touch locationInView:touch.view];
    CGPoint pastLocation = [touch previousLocationInView:touch.view];

    CGPoint d1 = CGPointMake(currentLocation.x-touch.view.center.x, currentLocation.y-touch.view.center.y);   
    CGPoint d2 = CGPointMake(pastLocation.x-touch.view.center.x, pastLocation.y-touch.view.center.y);

    CGFloat angle1 = atan2(d1.y, d1.x); 
    CGFloat angle2 = atan2(d2.y, d2.x); 
    [[ImgViewArrowImage layer] setAnchorPoint:CGPointMake(0.0, 0.5)];
    [[ImgViewArrowImage layer] setPosition:CGPointMake(159,211)];

    ImgViewArrowImage.transform = CGAffineTransformRotate(ImgViewArrowImage.transform, angle1-angle2);
  } 
}

最佳答案

我只是从我的代码中删除了两行,我的代码现在正在工作,所以最终代码是

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];        
    NSLog(@"%@",[touch view]);   
    if ([touch view] == ImgViewArrowImage) 
    {
    CGPoint d1 = [touch locationInView:touch.view];
    CGPoint d2 = [touch previousLocationInView:touch.view];


    CGFloat angle1 = atan2(d1.y, d1.x); 
    CGFloat angle2 = atan2(d2.y, d2.x); 
    [[ImgViewArrowImage layer] setAnchorPoint:CGPointMake(0.0, 0.5)];
    [[ImgViewArrowImage layer] setPosition:CGPointMake(159,211)];

    ImgViewArrowImage.transform = CGAffineTransformRotate(ImgViewArrowImage.transform, angle1-angle2);
  } 
}

关于iphone - 如何在iPhone的速度计中旋转图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11540894/

相关文章:

iphone - iTunes Connect 促销代码和应用程​​序更新

ios - 在 UILabel iOS 中点后显示带有两位小数的浮点值时遇到问题

objective-c - 是什么导致 Mac OS X 上找不到合适的图像错误?

objective-c - objective-c - 在消息传递应用程序中保存图像

ios - SKSMTPMessage 错误 - 操作无法完成。(OSStatus 错误 -9807。)

iphone - 禁用 UITextField 键盘?

ios - 转发选定的类以在另一个类中使用

iOS:在 UIBarButtonItem 和 Button 中附加带有标题的 FontAwesome 图标

iphone - objective-c - NSDateFormatter 返回错误的日期

iphone - UIWebView - 加载页面