在iPhone上根据触摸拖动旋转图像

9

我希望能够根据用户的触摸拖动及其速度,以顺时针或逆时针方向旋转图像。我认为这可以通过一些数学和逻辑来实现。请问有关此代码示例是什么样子的?

3个回答

16

如果你的目标是 iOS 3.2 或更高版本,你可以使用 UIRotationGestureRecognizer。代码看起来会像这样:

在你的设置方法中 (viewDidLoadinit 等):

UIRotationGestureRecognizer *rotationGesture = 
  [[UIRotationGestureRecognizer alloc] initWithTarget:self
                                               action:@selector(handleRotate:)];
rotationGesture.delegate = self;
[myImageView addGestureRecognizer:rotationGesture];
[rotationGesture release];

事件处理程序:

- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
  if(recognizer.state == UIGestureRecognizerStateBegan || 
     recognizer.state == UIGestureRecognizerStateChanged)
  {
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, 
                                                        recognizer.rotation);
    [recognizer setRotation:0];
  }
}

我在Github上有更多手势识别的示例(包括上面那个)。


2
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        currentTouch=touch;
        if (CGRectContainsPoint([self.view frame], [touch locationInView:self.ViewRotationArrow]))
        {
            [self transformSpinnerwithTouches:touch];
        }
        else if (!CGRectContainsPoint([ViewRotationArrow frame], [touch locationInView:self.ViewRotationArrow])) {
            [self dispatchTouchEvent:[touch view]WithTouch:touch];
        }
    }
}

-(void)transformSpinnerwithTouches:(UITouch *)touchLocation
{
    CGPoint touchLocationpoint = [touchLocation locationInView:self.view];
    CGPoint PrevioustouchLocationpoint = [touchLocation previousLocationInView:self.view];

    //Origin is the respective point. From that I am going to measure the
    //angle of the current position with respect to the previous position ....
    CGPoint origin;
    origin.x=240;
    origin.y=160;
    //NSLog(@"currentTouch Touch In Location In View:%F %F\n",touchLocationpoint.x,touchLocationpoint.y);
    //NSLog(@"currentTouch Touch previous Location In View:%F %F\n",PrevioustouchLocationpoint.x,PrevioustouchLocationpoint.y);
    CGPoint previousDifference = [self vectorFromPoint:origin toPoint:PrevioustouchLocationpoint];
    CGAffineTransform newTransform =CGAffineTransformScale(ViewRotationArrow.transform, 1, 1);
    CGFloat previousRotation = atan2(previousDifference.y, previousDifference.x);
    CGPoint currentDifference = [self vectorFromPoint:origin toPoint:touchLocationpoint];
    CGFloat currentRotation = atan2(currentDifference.y, currentDifference.x);
    CGFloat newAngle = currentRotation- previousRotation;
    NSLog(@"currentRotation of x %F  previousRotation %F\n",currentRotation,previousRotation);
    NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
    temp1=temp1+newAngle;
    //NSLog(@"Angle:%F\n",(temp1*180)/M_PI);
    newTransform = CGAffineTransformRotate(newTransform, newAngle);
    [self animateView:ViewRotationArrow toPosition:newTransform];
}

 -(CGPoint)vectorFromPoint:(CGPoint)firstPoint toPoint:(CGPoint)secondPoint
{
        CGPoint result;
        CGFloat x = secondPoint.x-firstPoint.x;
        CGFloat y = secondPoint.y-firstPoint.y;
        result = CGPointMake(x, y);
        return result;
}



-(void)animateView:(UIView *)theView toPosition:(CGAffineTransform) newTransform
{
    //animating the rotator arrow...
    [UIView setAnimationsEnabled:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.0750];
    ViewRotationArrow.transform = newTransform;
    [UIView commitAnimations];
}

这句话的意思是什么:“ViewRotationArrow”? - Rama Rao
@NIKHIL:这里的temp1是什么? - AppleDelegate
@AppleDelegate temp1 是基线的角度。假设为正X轴。 - NIKHIL
@NIKHIL 你好,我来晚了。在测试代码之前想问一下:你的解决方案是否考虑了自旋(不仅是在阻力下旋转)?此外,你能详细说明一下 temp1 吗?它在这里没有被用于计算,它的基准值是什么?提前感谢你。 - Unheilig
@unheiling 这段代码是三年前的,我猜你可以很容易地用Spritekit实现这个功能。我不确定这段代码是否能够正常工作。以下链接可能会给你更好的思路:-http://code4app.net/ios/rotate-to-select/50caf53e6803fa3e53000000 - NIKHIL
显示剩余2条评论

0
-(void)rotateView
{

    //shape of eclipse

    CAShapeLayer* circle = [[CAShapeLayer alloc] init];
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect ViewRect = CGRectMake(self.view.bounds.size.width/2.8, (self.view.bounds.size.height-self.view.bounds.size.width/2)/2, self.view.bounds.size.width/4, self.view.bounds.size.width/2);
    float midX = CGRectGetMidX(ViewRect);
    float midY = CGRectGetMidY(ViewRect);
    CGAffineTransform t = CGAffineTransformConcat(
                                                  CGAffineTransformConcat(
                                                                          CGAffineTransformMakeTranslation(-midX, -midY),
                                                                          CGAffineTransformMakeRotation(-1.57079633/0.99)),
                                                  CGAffineTransformMakeTranslation(midX, midY));
    CGPathAddEllipseInRect(path, &t, ViewRect);
    circle.path = path;
    circle.frame = self.view.bounds;
    circle.fillColor = [UIColor clearColor].CGColor;
    circle.strokeColor = [UIColor greenColor].CGColor;
    circle.lineWidth = 3.0f;
    [self.view.layer addSublayer:circle];

    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 0.0f;
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [circle addAnimation:animation forKey:@"strokeEnd"];


    // rotateView red color


    testView=[[UIView alloc]init];
    testView.frame=CGRectMake(0, 0, 20, 20);
    testView.backgroundColor=[UIColor redColor];
    testView.userInteractionEnabled=YES;
    testView.center=CGPathGetCurrentPoint(path);
    testView.transform = CGAffineTransformMakeRotation(1.57079633);
    [testView sizeToFit];
    [self.view.layer addSublayer:testView.layer];


    // view Animation

    CAKeyframeAnimation* ViewAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    ViewAnimation.duration = 15.0f;
    ViewAnimation.path = path;
    ViewAnimation.rotationMode = kCAAnimationRotateAuto;
    ViewAnimation.calculationMode = kCAAnimationCubicPaced;
    //ViewAnimation.removedOnCompletion = NO;
    [testView.layer addAnimation:ViewAnimation forKey:@"position"];

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接