views:

26

answers:

1

I have a UIImageView object that I rotate with frame property and CFAffineTransformMakeRotate and then I want ot move it with moving origin of its frame, but my image moves and reshape strangly.

 @implementation TimberView

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
 // Retrieve the touch point
 CGPoint pt = [[touches anyObject] locationInView:self];
 startLocation = pt;
 [[self superview] bringSubviewToFront:self];
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
 // Move relative to the original touch point
 CGPoint pt = [[touches anyObject] locationInView:self];
 CGRect frame = [self frame];

 frame.origin.x += pt.x - startLocation.x;
 frame.origin.y += pt.y - startLocation.y;
 [self setFrame:frame];
}

The TimberView class is a subclass of UIImageView

A: 

Quote from UIView's frame property reference:

If the transform property is also set, use the bounds and center properties instead; otherwise, animating changes to the frame property does not correctly reflect the actual location of the view.

So if you apply some custom transform to your view you cannot use view's frame property. To move the view change its center property instead, so your code should be transformed to (not sure if the code is correct or not):

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Move relative to the original touch point
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGPoint newCenter;

    newCenter.x += pt.x - startLocation.x;
    newCenter.y += pt.y - startLocation.y;
    [self setCenter: newCenter];
}

If you want just to position view's center to touched point you can use the following code:

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView: [self superview]];
self.center = touchPoint;
Vladimir
thnks, when i use your first code the shape blinks and don't move just in the direction of touch but your seccond code works, it has some little problems that I think I can solve them. (forexample when i touch a point on the shape, its center move to there and after that i can drag it)
vahid