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 +开发者_如何学Go 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.
精彩评论