[Code] By request, PocketTouch's track label transitions.

Discussion in 'iOS Development' started by SkylarEC, Apr 20, 2009.

  1. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    I found a much better way to handle things on the 3.0 firmware, so I don't mind throwing this code out there. all of the current PocketTouch will wind up open source eventualy (if al goes according to plan)

    Basically, what I do is take a UITransitionView and apply custom transitions to it. I use a combination of default UITransitionView animations, UIView animations, and Quartz' CATransitions. It should be pretty simple to figure out.


    Enjoy:
    Code:
    -(void)transitionTrackInfo:(id)param{
    	if(animatedTransitions){
    		if(randomTransitions){
    
    			CATransition *animation = [CATransition animation];
    
    			srandom([NSDate timeIntervalSinceReferenceDate]);
    			int transitionTypeRand = (random() % 4);
    			int transitionSubtypeRand = (random() % 4);
    	
    			switch (transitionTypeRand){
    				case 0:
    					animation.type = kCATransitionFade;
    					break;
    				case 1:
    					animation.type = kCATransitionPush;
    					break;
    				case 2:
    				case 3:
    				case 4:
    				case 5:
    					break;
    				default:
    					animation.type = kCATransitionFade;
    					break;
    			}
    
    
    			switch (transitionSubtypeRand){
    				case 0:
    					animation.subtype = kCATransitionFromLeft;
    					break;
    				case 1:
    					animation.subtype = kCATransitionFromRight;
    					break;
    				case 2:
    					animation.subtype = kCATransitionFromTop;
    					break;
    				case 3:
    					animation.subtype = kCATransitionFromBottom;
    					break;
    				default:
    					animation.subtype = kCATransitionFromLeft;
    					break;
    			}
    
    			animation.duration = 0.5;
    			animation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.75 :0.25 :0.75 :0.66];
    			animation.startProgress = 0;
    			animation.endProgress = 0.66;
    			animation.fillMode = kCAFillModeBoth;
    			[[labelTransition _layer] addAnimation:animation forKey:kCATransition];
    
    			switch (currentContainer){
    				case 1:
    					switch(transitionTypeRand){
    						case 0:
    						case 1:
    							[labelTransition transition:10 toView:labelContainer2];
    							break;
    						case 2:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:labelTransition cache:NO];
    							[labelContainer1 removeFromSuperview];
    							[labelTransition addSubview:labelContainer2];
    							[UIView commitAnimations];
    							break;
    						case 3:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:labelTransition cache:NO];
    							[labelContainer1 removeFromSuperview];
    							[labelTransition addSubview:labelContainer2];
    							[UIView commitAnimations];
    							break;
    						case 4:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:labelTransition cache:NO];
    							[labelContainer1 removeFromSuperview];
    							[labelTransition addSubview:labelContainer2];
    							[UIView commitAnimations];
    							break;
    						case 5:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:labelTransition cache:NO];
    							[labelContainer1 removeFromSuperview];
    							[labelTransition addSubview:labelContainer2];
    							[UIView commitAnimations];
    							break;
    					}
    					break;
    				case 2:
    					switch(transitionTypeRand){
    						case 0:
    						case 1:
    							[labelTransition transition:10 toView:labelContainer1];
    							break;
    						case 2:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:labelTransition cache:NO];
    							[labelContainer2 removeFromSuperview];
    							[labelTransition addSubview:labelContainer1];
    							[UIView commitAnimations];
    							break;
    						case 3:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:labelTransition cache:NO];
    							[labelContainer2 removeFromSuperview];
    							[labelTransition addSubview:labelContainer1];
    							[UIView commitAnimations];
    							break;
    						case 4:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:labelTransition cache:NO];
    							[labelContainer2 removeFromSuperview];
    							[labelTransition addSubview:labelContainer1];
    							[UIView commitAnimations];
    							break;
    						case 5:
    							[UIView beginAnimations:nil];
    							[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    							[UIView setAnimationDuration:0.66];
    							[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:labelTransition cache:NO];
    							[labelContainer2 removeFromSuperview];
    							[labelTransition addSubview:labelContainer1];
    							[UIView commitAnimations];
    							break;
    					}
    					break;
    				default:
    					[labelTransition transition:10 toView:pauseContainer];
    					break;
    			}
    
    		}else{
    			switch (currentContainer){
    				case 1:
    					[labelTransition transition:6 toView:labelContainer2];
    					break;
    				case 2:
    					[labelTransition transition:6 toView:labelContainer1];
    					break;
    				default:
    					[labelTransition transition:6 toView:pauseContainer];
    					break;
    			}
    		}
    	}else{
    		switch (currentContainer){
    			case 1:
    				[labelTransition transition:0 toView:labelContainer2];
    				break;
    			case 2:
    				[labelTransition transition:0 toView:labelContainer1];
    				break;
    			default:
    				[labelTransition transition:0 toView:pauseContainer];
    				break;
    		}
    	}		
    }




    PS, the new way I'm doing this takes less about 1/3 the memory : )
    [EDIT] And SUBSTANTIALLY less code!

Share This Page