2010-12-02 76 views
1

我有一個在iPhone SDK中拖動對象的問題,我的UIImageView插座是紅色的,我不知道爲什麼拖動不起作用!但是,當刪除,如果行我的代碼工作正常,這裏是代碼:拖動特定的對象

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 


    UITouch *touch = [[event allTouches]anyObject]; 

    if ([touch view] == red) { 
    CGPoint location = [touch locationInView:self.view]; 
    red.center = location; 

    } 
} 


- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

     [self touchesBegan:touches withEvent:event]; 
    } 
+1

您必須在CGTRE「touchesMoved」上寫下您的對象的位置 – Momi 2010-12-02 12:23:27

回答

1

當你重寫一個UIView子類的touchesBegan,只提供給UIView的觸摸被處理那裏。

對於它的子視圖,您需要將UserInteractionEnabled設置爲TRUE。然後爲您的子視圖創建一個子類,通過它可以將觸摸處理委託給您的SuperView。

如果這聽起來很複雜,只需使用 - (UIView的*)則hitTest:(CGPoint)點withEvent:方法(*的UIEvent)事件

+0

謝謝,momeks權利! ,我想在屏幕上拖動這個對象 – 2010-12-02 12:40:59

0

您好我用下面的代碼做了。我必須拖動imageEnd在特定的水平和垂直方式,所以我把它們如下所示。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView: touch.view]; 
location.x=510.0; 
[imgEndPoint setUserInteractionEnabled:TRUE]; 

if ([touch view] == imgEndPoint) { 
    if (location.y < 710.0 && location.y > 75.0) {//image move between this coordinate 
     imgEndPoint.center = location; 
    } 
} 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

UITouch *touch = [[event allTouches] anyObject]; 

if ([touch view] == imgEndPoint) { 

    CGPoint location = [touch locationInView: self.view]; 
    location.x=510.0; 
    if (location.y < 710.0 && location.y > 75.0) { 
    imgEndPoint.center = location; 
     [self getPixcel]; 
    } 
    [imgEndPoint setUserInteractionEnabled:TRUE]; 
} 
}