2010-08-12 93 views
0

我有一個UIImageView對象,我與框架屬性和CFAffineTransformMakeRotate旋轉,然後我想ot移動它的框架的移動原點,但我的圖像移動和strangly重塑。Draggable UIImgeView Rotation

@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]; }

的TimberView類是的UIImageView

的一個子類
+0

你可以將代碼發佈到移動視圖的位置嗎? – Vladimir 2010-08-12 09:38:03

+0

我已經發布了它。 – vahid 2010-08-14 08:18:39

回答

0

引自的UIView的幀屬性參考:

如果變換屬性也 集,使用的邊界和中心 屬性,而不是;否則, 動畫更改爲幀 屬性不能正確反映 視圖的實際位置。

因此,如果您將一些自定義轉換應用於視圖,則無法使用視圖的框架屬性。要移動視圖改變其center屬性代替,所以你的代碼轉化爲(不知道如果代碼正確與否):

- (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]; 
} 

如果你只想定位視圖對觸摸點,您可以使用中心以下代碼:

UITouch *touch = [touches anyObject]; 
CGPoint touchPoint = [touch locationInView: [self superview]]; 
self.center = touchPoint; 
+0

thnks,當我使用你的第一個代碼的形狀閃爍,不只是在觸摸的方向移動,但你的第二代碼的作品,它有一些小問題,我認爲我可以解決它們。 (例如當我觸摸形狀上的一個點時,它的中心移動到那裏,之後我可以拖動它) – vahid 2010-08-15 06:44:20