2012-05-30 16 views
28

मैं मानक CGContextFillEllipseInRect() कोड का उपयोग कर UIView के -drawRect: विधि में एक वृत्त खींच रहा हूं। हालांकि, मैं थोड़ा पल्स (बड़ा और छोटा बनाना) और एनीमेशन के साथ रंग भरने की तीव्रता को बदलना चाहता हूं। उदाहरण के लिए, यदि सर्कल लाल से भरा हुआ है, तो मैं सर्कल को पल्स करना चाहता हूं और लाल रंग को थोड़ा हल्का और गहराई से पल्सिंग एक्शन के साथ बनाना चाहता हूं। कोर एनीमेशन के साथ ज्यादा अनुभव नहीं है, मैं इसे कैसे करना है इसके बारे में थोड़ा सा खो गया हूं, इसलिए किसी भी मदद की सराहना की जाएगी।drawRect सर्कल और एनिमेट आकार/रंग

उत्तर

70

यदि आप drawRect: में सर्कल नहीं खींचते हैं तो यह बहुत आसान है।

@implementation PulseView 

+ (Class)layerClass { 
    return [CAShapeLayer class]; 
} 

प्रणाली हुए अपने दृश्य पर layoutSubviews भेजता है जब भी यह (सहित जब वह पहली बार दिखाई देता है) आकार बदलता है: इसके बजाय, एक CAShapeLayer, इस तरह उपयोग करने के लिए आपके विचार की स्थापना की। हम layoutSubviews ओवरराइड आकार स्थापित करने के लिए और चेतन यह:

- (void)setLayerProperties { 
    CAShapeLayer *layer = (CAShapeLayer *)self.layer; 
    layer.path = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath; 
    layer.fillColor = [UIColor colorWithHue:0 saturation:1 brightness:.8 alpha:1].CGColor; 
} 

हम की जरूरत है:

- (void)layoutSubviews { 
    [self setLayerProperties]; 
    [self attachAnimations]; 
} 

यहाँ कैसे हम परत के पथ (जो अपने आकार को निर्धारित करता है) और आकार के लिए रंग भरने सेट है

- (void)attachAnimations { 
    [self attachPathAnimation]; 
    [self attachColorAnimation]; 
} 

यहाँ कैसे हम परत के पथ चेतन है::

रंग भरने के लिए पथ के लिए और दूसरा - परत करने के लिए दो एनिमेशन देते हैं

- (void)attachColorAnimation { 
    CABasicAnimation *animation = [self animationWithKeyPath:@"fillColor"]; 
    animation.fromValue = (__bridge id)[UIColor colorWithHue:0 saturation:.9 brightness:.9 alpha:1].CGColor; 
    [self.layer addAnimation:animation forKey:animation.keyPath]; 
} 

attach*Animation तरीकों में से दोनों एक सहायक विधि है कि एक बुनियादी एनीमेशन बनाता है और यह सेट अप autoreverse और एक एक सेकंड की अवधि के साथ अनिश्चित काल को दोहराने के लिए उपयोग करें:

- (void)attachPathAnimation { 
    CABasicAnimation *animation = [self animationWithKeyPath:@"path"]; 
    animation.toValue = (__bridge id)[UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 4, 4)].CGPath; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    [self.layer addAnimation:animation forKey:animation.keyPath]; 
} 

यहाँ कैसे हम परत के भरण रंग चेतन है :

- (CABasicAnimation *)animationWithKeyPath:(NSString *)keyPath { 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath]; 
    animation.autoreverses = YES; 
    animation.repeatCount = HUGE_VALF; 
    animation.duration = 1; 
    return animation; 
} 
+0

बहुत बढ़िया स्पष्टीकरण। बहुत बहुत धन्यवाद :) – Skoota

+1

यह वही है जो मैं ढूंढ रहा हूं, लेकिन इसे दिखाने के लिए मैं इसे UIView से कैसे कॉल करूं? मुझे लगता है कि यह कोड अपनी कक्षा में स्थापित है। – bmueller

+4

'पल्स व्यू' 'UIView' का उप-वर्ग है। –

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