2010-12-20 12 views
10

मैं एक टिंटकॉलर से दूसरे में बदलने के लिए, एक यूआईटीूलबार की टिंटकॉलर संपत्ति को एनिमेट करने की कोशिश कर रहा हूं।क्या मैं UIToolbar के टिंटकॉलर को फीका/एनिमेट कर सकता हूं?

यहां कोड है जो मैं कोशिश कर रहा हूं। दुर्भाग्य से, परिवर्तन तुरंत होता है और हरे से नीले रंग में फीका नहीं होता है। यह अजीब बात है क्योंकि मुझे ऐप्पल फीड्स और "दालें" टूलबार टिंट रंग पता है जब टेदरिंग या फोन कॉल पर। तो यह क्यों काम नहीं करता है?

// set initial tint color 
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.95 blue:0.15 alpha:0.6]; 

//animation stuff 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.95]; 
[UIView setAnimationDelegate:self];   

//thing to animate 
myBottomToolBar.tintColor = [UIColor colorWithRed:0.15 green:0.35 blue:0.45 alpha:0.6]; 

//animation stuff 
[UIView commitAnimations]; 
+0

WSYIWYG संपादक में बाइनरी बटन के माध्यम से अपना कोड चिपकाएं। इस तरह यह अपनी स्वरूपण रखता है। – TALLBOY

उत्तर

3

सार्वजनिक एपीआई के माध्यम से एनिमेटेबल में टिंट रंग नहीं। आप टाइमर पर मैन्युअल रूप से रंग रंग बदलकर इसे चारों ओर काम कर सकते हैं। आपको मध्यवर्ती रंग के स्तर को अलग करना होगा।

0

भविष्य के संदर्भ के लिए, यहां टिंट एनिमेट करने के लिए मेरा कुछ जल्दबाजी समाधान है। मुझे यकीन है कि श्रेणियों और आरजीबी मूल्यों yadda yadda के लिए एक संरचना के साथ ऐसा करने के अधिक चालाक तरीके हैं। मैं जल्दी में था, ठीक है ?!

UITabBarController उपवर्ग:

@interface BaseNavigationController : UINavigationController 
{ 
    NSTimer *   tintTimer; 
    UIColor *   targetColor; 
} 

-(void)changeTintTo:(UIColor*)color; 
-(void)animateTint; 

-(void)changeTintTo:(UIColor*)color; 
{ 
    targetColor = [color retain]; 

    [tintTimer invalidate]; 
    tintTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(animateTint) userInfo:nil repeats:YES]; 

} 

-(void)animateTint; 
{ 
    UIColor * currentColor = self.navigationBar.tintColor; 

    CGFloat red, green, blue, alpha; 
    [currentColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

    CGFloat targetRed, targetGreen, targetBlue, targetAlpha; 
    [targetColor getRed:&targetRed green:&targetGreen blue:&targetBlue alpha:&targetAlpha]; 

    CGFloat newRed = red + ((targetRed - red)*.2); 

    UIColor * newColor = [UIColor colorWithRed:newRed 
             green:green + ((targetGreen - green)*.2) 
              blue:blue + ((targetBlue - blue)*.2) // the .2 adjusts the fade speed 
             alpha:1.0]; 

    if(
     (newRed < targetRed && newRed >= targetRed-0.01) || 
     (newRed > targetRed && newRed <= targetRed+0.01) 
    ) 
    { 
     newColor = targetColor; 
     [targetColor autorelease]; 
     [tintTimer invalidate]; 
     tintTimer = nil; 
     targetColor = nil; 
    } 

    self.navigationBar.tintColor = newColor; 

} 
+0

आपको एनिमेशन के लिए 'NSTimer' की बजाय' CADisplayLink' का उपयोग करना चाहिए। – rounak

0

NSTimer और यह सेटअप एक साधारण एनीमेशन के लिए बहुत काम है।

-(void)animateItemToTargetAlpha:(CGFloat)targetAlpha 
{ 
    static dispatch_source_t timer = nil; 
    static CGFloat DURATION = 0.25f; 
    static CGFloat FPS = 30.f; 
dispatch_source_cancel(timer); 
    timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue()); 
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1/FPS * NSEC_PER_SEC,(1ull * NSEC_PER_SEC)/10); 

    CGFloat red, green, blue, __block alpha; 
    [self.navigationItem.rightBarButtonItem.tintColor getRed:&red green:&green blue:&blue alpha:&alpha]; 

    dispatch_source_set_event_handler(timer, ^{ 
     alpha = targetAlpha == 1.0f ? alpha + (1/(FPS * DURATION)) : alpha - (1/(FPS * DURATION)); 
     self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 
     if(alpha >= 1 || alpha <= 0) 
     { 
      dispatch_source_cancel(timer); 
     } 
    }); 

    dispatch_resume(timer); 
} 

रैखिक वक्र एक छोटे से ध्यान देने योग्य हो सकता है, लेकिन मैं CADisplayLink पहले की कोशिश कर रहा में दिलचस्पी रखता हूँ: यहाँ मेरी समाधान का उपयोग कर प्रेषण, मैं सिर्फ टिंट रंग के अल्फा मान बदलकर अंदर और बाहर एक UIBarButtonItem fading हूँ कोई बदलाव करने से पहले।

संबंधित मुद्दे