简体   繁体   中英

iPhone coding - How to add a rotation to the touch event?

Ho can i add a rotation to the object addition to the dragging ?

i dosen't have to be multi touch.

code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:self.view];
    CGFloat dx = currentTouch.x - carMove.position.x;
    CGFloat dy = currentTouch.y - carMove.position.y;
    CGFloat dist = sqrt(dx * dx + dy * dy);
    if(dist <carMove.radius) {
        carMove.velocity = CGPointMake(0.0, 0.0);
        carMove.dragging = YES;
    }

    lastTouch = currentTouch;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:self.view];
    if( carmove.dragging ){
        carMove.position = currentTouch;
        carMove.velocity = CGPointMake(currentTouch.x - lastTouch.x, currentTouch.y - lastTouch.y);
    }
    lastTouch = currentTouch;
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    carMove.dragging = NO;
}

Try something like

for (int c=0; c < imagesArray.count; c++) {

  MyCustomImageObject img = [imagesArray objectAtIndex:c];

  CGContextSaveGState(context);

  CGContextTranslateCTM(context,
    img.centreOfRotatedImageInBottomLeftPixelCoordsX,
    img.centreOfRotatedImageInBottomLeftPixelCoordsY);

  CGContextRotateCTM(context, img.floatingPointImageAngleInRadians);

  CGContextDrawImage(context,
    CGMakeRect(-img.width/2, -img.height/2, +img.width/2, +img.height/2),
    image.CGImage);

  CGContextRestoreGState(context);
}

Or if you want to have one view per image, use code like that inside the loop once in the drawRect: method of your custom image views.

EDIT

You need to register for the accelerometer events if you want to base rotation on phone rotation. You'll need to apply a low-pass filter, like Apple suggest in the docs, in the function that receives the events:

#define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
  (UIAcceleration *)acceleration {
  // Use a basic low-pass filter to keep only the gravity component of each axis.
  accelX = (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor));
  accelY = (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor));
  accelZ = (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor));
}

Then simply do an atan(accelY,accelX), set it on the object being moved and call setNeedsDisplay.

If the performance isn't high enough, you could try testing the CGRect sent to drawRect and only redrawing images that are within the redraw rectangle;, but beware, if the rectangle has moved, you'll also want to (scrub out/erase) the rectangle it was at just before it moved, so keep a record of those and make a larger repaint rectangle from the minimum and maximum X and Y values in old and new rectangles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM