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:
Is this a problem with my code, or is it with Color Blended Layers/Core Animation?
Thanks
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: