in Viewdidload where CandidateView is custom viewcontroller
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];
}
}
This is the method
-(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);
}
From this code i am able to move a button in the CandidateView.. Now i want to drag whole CandidateView or should I make UIView instead of UIViewcontroller.
Can anyone help me??
Thanks in advance..:)
Make a UIView and put everything inside of that, the UIView should fill the screen of your “UIViewController”, the dragging method should work fine for that too 🙂 Good luck! (*Another solution you might want to look into is making a UIScrollView that contains all of your objects nested in it)