2013-05-28 42 views
0

我已經創建了一個類Box,它是UIImageView的子類。可拖動的特定類,它是UIImageView的子類

在我的程序中,我想讓Box成爲主視圖控制器中唯一可拖動的對象。

這裏是我的代碼:

BoxViewController.m

-(void) viewDidLoad{ 
    [super viewDidLoad]; 
    target = [[Box alloc] initWithFrame:CGRectMake(10,10,100,100)]; 
    [self.view addSubView:target]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    if([touch view] == target){ 
      CGPoint location = [touch locationInView:self.view]; 
      target.center = location; 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
    [self touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event]; 
} 

我認爲條件不工作,但我已經試過isKindOfClass。兩者都不起作用。解決辦法是什麼?非常感謝你。

+0

你有沒有設定目標的userInteractionEnabled是YES? –

回答

0

試試這個方法

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    CGPoint location = [[touches anyObject] locationInView:self.view]; 
    target.transform = CGAffineTransformMakeTranslation(location.x, location.y); 
} 
相關問題