1 inline CAAnimation* 2 BounceAnim ationEleme nt( CGPoint pFrom, CGPoint pTo, double pBeginTime , double pDuration, CAMediaTim ingFunctio n* pMTF ) 3 { CABasicAni mation* v = [ CABasicAni mation animationW ithKeyPath :@"position" ]; 4 v.fromValue = [ NSValue valueWithC GPoint:pFrom ]; 5 v.toValue = [ NSValue valueWithC GPoint:pTo ]; 6 v.beginTime = pBeginTime ; 7 v.duration = pDuration; 8 v.timingFunc tion = pMTF; 9 return v; 10 } 11 12 inline CAAnimatio n* 13 BounceAnim ation( CALayer* pL, CGPoint pDest, double pInitialDu ration = 1, double pK = .3, size_t pRepeat = 10 ) 14 { CAMediaTim ingFunctio n* wMTF_D = [ CAMediaTim ingFunctio n functionWi thControlP oints:1./3. :0 :2./3. :1./3. ]; 15 CAMediaTim ingFunctio n* wMTF_U = [ CAMediaTim ingFunctio n functionWi thControlP oints:1./3. :2./3. :2./3. :1 ]; 16 17 CGPoint wOrigin = pL.position; 18 19 NSMutableA rray* wAnimation Array = NSMutableA rray.array; 20 [ wAnimation Array addObject:BounceAnim ationEleme nt( pL.position, pDest, 0, pInitialDu ration, wMTF_D ) ]; 21 22 double wBeginTime = pInitialDu ration; 23 24 double wK = 1; 25 while ( pRepeat-- ) 26 { wK *= pK; 27 CGPoint wPoint = CGPointMak e 28 ( wOrigin.x * wK + pDest.x * ( 1 - wK ) 29 , wOrigin.y * wK + pDest.y * ( 1 - wK ) 30 ); 31 double wDuration = pInitialDu ration * wK * ( 1 / sqrt( wK ) ); 32 [ wAnimation Array addObject:BounceAnim ationEleme nt( pDest, wPoint, wBeginTime , wDuration, wMTF_U ) ]; 33 wBeginTime += wDuration; 34 [ wAnimation Array addObject:BounceAnim ationEleme nt( wPoint, pDest, wBeginTime , wDuration, wMTF_D ) ]; 35 wBeginTime += wDuration; 36 } 37 38 pL.position = pDest; 39 40 CAAnimatio nGroup* v = CAAnimatio nGroup.animation; 41 v.duration = wBeginTime ; 42 v.animations = wAnimation Array; 43 44 return v; 45 } 46 47 - (IBAction) 48 Group 49 { [ mL addAnimati on:BounceAnim ation( mL, CGPointMak e( 160, 240 ) ) forKey:nil ]; 50 } 51
posted by
Saturn
on Thu 19 Sep 2013
at 11:18