Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

oxcug

macrumors newbie
Original poster
Oct 3, 2011
24
0
I'm trying to figure out how to allow the user to move objects around the screen with their finger. I've seen some tutorials that do this with success but they don't work with multiple objects on the screen.
 
Subclass an UIView of your own, implement the drag code, and send a delegate to your main window where you add them. i did this for some kid games, works like a charm.

Can you show me the code for the dragging?
 
Can you show me the code for the dragging?

It's all arond the internet, but i'm in a good mood so here.

Code:
- (void) setup {
    imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    imageView.image = [UIImage imageNamed:self.imageName];
    [self addSubview:imageView];
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureMoveAround:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    panRecognizer.delegate = self;
    [self addGestureRecognizer:panRecognizer];
    [panRecognizer release];
}

-(void)panGestureMoveAround:(UIPanGestureRecognizer *)gesture;
{
    UIView *piece = [gesture view];
    [self bringSubviewToFront:piece];
    CGPoint translation = [gesture translationInView:[piece superview]];
    if ([gesture state] == UIGestureRecognizerStateChanged) {
        [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y+translation.y)];
        [gesture setTranslation:CGPointZero inView:[piece superview]];
    } else if ([gesture state] == UIGestureRecognizerStateEnded) {
        [_delegate subclassView:self panningDidEnd:CGPointMake(self.frame.origin.x, self.frame.origin.y)];
    }
}

This is also included the custom delegate messages to the main view, this is inside the subclassed UIView.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.