在 Viewdidload 中,CandidateView 是自定义视图控制器
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。谁能帮我??提前致谢..:)