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

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi, In my app I have a method system for the user to drag UIImageViews on the screen. The problem is, that when a user drags an image, it is choppy and stops in the middle. Here is my code:
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    marblebeingdragged = NO;
    UITouch *touch = [[event touchesForView:self.view] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    
    
    if(CGRectContainsPoint(plusone.frame, location) && marblebeingdragged == NO)  //Drag Plus One
    {
        marblebeingdragged = YES;
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plusone.center = location;
    }
    if(CGRectContainsPoint(plustwo.frame, location) && marblebeingdragged == NO) //Drag Plus Two
    {
        
        marblebeingdragged = YES;
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plustwo.center = location;
        
    }
    if(CGRectContainsPoint(plusthree.frame, location) && marblebeingdragged == NO) //Drag Plus Three
    {
        
        marblebeingdragged = YES;
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        plusthree.center = location;
        
    }
    if(CGRectContainsPoint(minusone.frame, location) && marblebeingdragged == NO) //Drag Minus One
    {
        
        marblebeingdragged = YES;
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minusone.center = location;
        
    }
    if(CGRectContainsPoint(minustwo.frame, location) && marblebeingdragged == NO) //Drag Minus Two
    {
        marblebeingdragged = YES;
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minustwo.center = location;
        
    }
    if(CGRectContainsPoint(minusthree.frame, location) && marblebeingdragged == NO) //Drag Minus Three
    {
        marblebeingdragged = YES;
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint location = [touch locationInView:touch.view];
        minusthree.center = location;
    }
}

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

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    if (CGRectIntersectsRect(jar.frame, plusone.frame)) { //Plus One Collision
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //iPhone Vibrate
        
        marbles = 1+marbles; // Add 1 To Marbles
        marblesleft = marblesneeded - marbles;
        marblesNeeded.text = [NSString stringWithFormat:@"%i", marblesleft];
        NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
        
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
                                                 (CFStringRef) @"1 Marble", CFSTR ("aif"), NULL);
        
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
        
        [[records objectAtIndex:pageControl.currentPage] setValue:marbleNumber forKey:@"marbles"];
        [self checkPrizeReached];
        NSError *error;
        [_managedObjectContext save:&error]; //Save 
        
        [UIView beginAnimations:nil context:nil]; //Move To Start Position With Animation
        [UIView setAnimationDuration:0.5];
        plusone.center = CGPointMake(36, 46);
        [UIView commitAnimations];
        marblebeingdragged = NO;
    } else
    {
        marblebeingdragged = YES;
    }
    if (CGRectIntersectsRect(jar.frame, plustwo.frame)) { //Plus Two Collision
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //iPhone Vibrate
        
        marbles = 2+marbles;
        marblesleft = marblesneeded - marbles;
        marblesNeeded.text = [NSString stringWithFormat:@"%i", marblesleft];
        NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
        
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
                                                 (CFStringRef) @"2 Marbles", CFSTR ("aif"), NULL);
        
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
        

        [[records objectAtIndex:pageControl.currentPage] setValue:marbleNumber forKey:@"marbles"];
        [self checkPrizeReached];

        NSError *error;
        [_managedObjectContext save:&error]; //Save 
                
        [UIView beginAnimations:nil context:nil]; //Move To Start Position With Animation
        [UIView setAnimationDuration:0.5];
        plustwo.center = CGPointMake(36, 134);
        [UIView commitAnimations];
        marblebeingdragged = NO;
        
    }
    else{
        marblebeingdragged = YES;
    }
    if (CGRectIntersectsRect(jar.frame, plusthree.frame)) { //Plus Three Collision
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //iPhone Vibrate
        
        marbles = 3+marbles;
        marblesleft = marblesneeded - marbles;
        marblesNeeded.text = [NSString stringWithFormat:@"%i", marblesleft];
        NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
        
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, 
                                                 (CFStringRef) @"3 Marbles", CFSTR ("aif"), NULL);
        
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID);
        

        [[records objectAtIndex:pageControl.currentPage] setValue:marbleNumber forKey:@"marbles"];
        [self checkPrizeReached];

        NSError *error;
        [_managedObjectContext save:&error]; //Save 
        
        [UIView beginAnimations:nil context:nil]; //Move To Start Position With Animation
        [UIView setAnimationDuration:0.5];
        plusthree.center = CGPointMake(36, 222);
        [UIView commitAnimations];
        marblebeingdragged = NO;
        
    }
    else{
        marblebeingdragged = YES;
    }
    if (CGRectIntersectsRect(jar.frame, minusone.frame)) { //Minus One Collision
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //iPhone Vibrate
        
        marbles = marbles-1;
        marblesleft = marblesneeded - marbles;
        marblesNeeded.text = [NSString stringWithFormat:@"%i", marblesleft];
        NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
        [[records objectAtIndex:pageControl.currentPage] setValue:marbleNumber forKey:@"marbles"];
        [self checkPrizeReached];

        NSError *error;
        [_managedObjectContext save:&error]; //Save 
        
        [UIView beginAnimations:nil context:nil]; //Move To Start Position With Animation
        [UIView setAnimationDuration:0.5];
        minusone.center = CGPointMake(282, 46);
        [UIView commitAnimations];
        marblebeingdragged = NO;
        
    }
    else{
        marblebeingdragged = YES;
    }
    if (CGRectIntersectsRect(jar.frame, minustwo.frame)) { //Minus Two Collision
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //iPhone Vibrate

        marbles = marbles-2;
        marblesleft = marblesneeded - marbles;
        marblesNeeded.text = [NSString stringWithFormat:@"%i", marblesleft];
        NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
        [[records objectAtIndex:pageControl.currentPage] setValue:marbleNumber forKey:@"marbles"];
        [self checkPrizeReached];

        NSError *error;
        [_managedObjectContext save:&error]; //Save 
        
    
        [UIView beginAnimations:nil context:nil]; //Move To Start Position With Animation
        [UIView setAnimationDuration:0.5];
        minustwo.center = CGPointMake(282, 134);
        [UIView commitAnimations];
        marblebeingdragged = NO;
        
    }
    
    if (CGRectIntersectsRect(jar.frame, minusthree.frame)) { //Minus Three Collision
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); //iPhone Vibrate
        
        marbles = marbles-3;
        marblesleft = marblesneeded - marbles;
        marblesNeeded.text = [NSString stringWithFormat:@"%i", marblesleft];
        NSNumber *marbleNumber = [NSNumber numberWithInt:marbles];
        [[records objectAtIndex:pageControl.currentPage] setValue:marbleNumber forKey:@"marbles"];
        [self checkPrizeReached];

        NSError *error;
        [_managedObjectContext save:&error]; //Save 
        
        [UIView beginAnimations:nil context:nil]; //Move To Start Position With Animation
        [UIView setAnimationDuration:0.5];
        minusthree.center = CGPointMake(282, 222);
        [UIView commitAnimations];
        marblebeingdragged = NO;
        
    }
    else{
        marblebeingdragged = YES;
    }
}

Is this a problem with my code, or is it with Color Blended Layers/Core Animation?

Thanks:)
 
Last edited:
Auwch, i'd recommand using GestureRecognizers.
Make a subclass of an UIView, which you call MarbleView.
It should extend from UIView
And you give it a class to assign it's image, and others, that's what you decide.
After, it's easy as this.

Code:
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureMoveAround:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    panRecognizer.delegate = self;
    [self addGestureRecognizer:panRecognizer];
    [panRecognizer release];
That's for the panning around.
And in that piece of code, i do use my own delegate, so you should work around that. But here it is.

Code:
-(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 vormenView:self panningDidEnd:CGPointMake(self.frame.origin.x, self.frame.origin.y)];
    }
}

PS: after in your code, you can just Add MarbleView as object, and just drag it around ^_- Will save you alot of coding. Subclassing that is.
 
Auwch, i'd recommand using GestureRecognizers.
Make a subclass of an UIView, which you call MarbleView.
It should extend from UIView
And you give it a class to assign it's image, and others, that's what you decide.
After, it's easy as this.

Code:
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureMoveAround:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    panRecognizer.delegate = self;
    [self addGestureRecognizer:panRecognizer];
    [panRecognizer release];
That's for the panning around.
And in that piece of code, i do use my own delegate, so you should work around that. But here it is.

Code:
-(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 vormenView:self panningDidEnd:CGPointMake(self.frame.origin.x, self.frame.origin.y)];
    }
}

PS: after in your code, you can just Add MarbleView as object, and just drag it around ^_- Will save you alot of coding. Subclassing that is.

Will your code work with multiple image views? If yes, where do I implement your code?

Thanks for your response!:)
 
Ofcourse it will, the thing is, you have to create your own "UIImageView", so not the standard UIImageView.
If you create a subclass of them, and just call them instead of your UIImageView, your code will apply to every one you instantiate. This is some Obj-C coding, you might want to read up on Subclassing Classes.
Then you will understand what I am talking about.
And you will see that my code will work on every one ;)
It's close to copy/paste my stuff if you know what I'm talking about.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.