views:

376

answers:

1

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;
}
A: 

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.

martinr
this will make a full rotation, no? i need the the user will rotate the image object as needed (not having to be a full rotation).
omri
The rotation will be what the user rotates to phone to, yes. If you want to make the phone rotation control changes to the rotation relative to the rotation when the user started rotating an object, store the original rotation of the image and the phone in your image object and only modify the image rotation with changes from the initial state instead.
martinr
Oh and please vote me up AND/OR mark this as the best answer if you find this useful. Thx
martinr
i need that the user will rotate the object by touching the object, how can i do that?
omri
Keep a record of the last image touched, and whether the last event was UITouchEventBegan, UITouchEventCancelled, UITouchEventMoved or UITouchEventEnded, and what the phone rotation was when the finger last went down in your app delegate. Update the values when you get a touch event or accelerometer event. Also in the event handler (accelerometer:didAccelerate:), test if the last event was UITouchEventMoved or UITouchEventBegan and if so update the rotation of the current image to be that of the phone, or of the difference between the rotation now and when the user's finger last went down.
martinr