2013-02-14 21 views
1

在viewDidLoad中,其中CandidateView是自定義視圖 - 控制UIControlEventTouchDragInside問題

for (int i = 0; i < 9 ; i++) { 
    for (int j = 0; j < 3; j++) { 
     CandidateView *candView = [[CandidateView alloc]init]; 
     candView.view.frame = CGRectMake(15 + i*186 ,17 + j*145, 158,130); 
     [candView.btnCandidate addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
     [mainScroll addSubview:candView.view]; 
    } 
} 

這是方法

-(void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event 
{ 

    UITouch *touch = [[event touchesForView:button] anyObject]; 
    // get delta 
    CGPoint previousLocation = [touch previousLocationInView:button]; 
    CGPoint location = [touch locationInView:button]; 
    CGFloat delta_x = location.x - previousLocation.x; 
    CGFloat delta_y = location.y - previousLocation.y; 

    // move button 
    button.center = CGPointMake(button.center.x + delta_x,button.center.y + delta_y); 
} 

從這個代碼,我能夠在CandidateView移動按鈕..現在我想拖整個CandidateView或應該使UIView而不是UIViewcontroller。 任何人都可以幫助我嗎? 在此先感謝.. :)

回答

1

製作一個UIView,並把所有內容,UIView應該填充你的「UIViewController」的屏幕,拖動方法應該工作得很好:)太好了! (*你可能想要研究的另一個解決方案是製作一個包含嵌套在其中的所有對象的UIScrollView)