मनी-बैक गारंटीकृत, प्रबलित-ठोस-ठोस तरीके से समकालिक रूप से आकर्षित करने के लिए मजबूर करना (कॉलिंग कोड पर लौटने से पहले)CALayer आपके UIViewउपवर्ग के साथ बातचीत को कॉन्फ़िगर करना है ।
अपने यूविवि उपवर्ग में, एक displayNow()विधि बनाएं जो परत को " प्रदर्शन के लिए सेट " करने के लिए कहता है, फिर " ऐसा करने के लिए ":
तीव्र
/// Redraws the view's contents immediately.
/// Serves the same purpose as the display method in GLKView.
public func displayNow()
{
let layer = self.layer
layer.setNeedsDisplay()
layer.displayIfNeeded()
}
उद्देश्य सी
/// Redraws the view's contents immediately.
/// Serves the same purpose as the display method in GLKView.
- (void)displayNow
{
CALayer *layer = self.layer;
[layer setNeedsDisplay];
[layer displayIfNeeded];
}
एक ऐसी draw(_: CALayer, in: CGContext)विधि भी लागू करें जो आपके निजी / आंतरिक ड्राइंग विधि को बुलाएगी (जो कि हर UIViewएक के बाद काम करती है CALayerDelegate) :
तीव्र
/// Called by our CALayer when it wants us to draw
/// (in compliance with the CALayerDelegate protocol).
override func draw(_ layer: CALayer, in context: CGContext)
{
UIGraphicsPushContext(context)
internalDraw(self.bounds)
UIGraphicsPopContext()
}
उद्देश्य सी
/// Called by our CALayer when it wants us to draw
/// (in compliance with the CALayerDelegate protocol).
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
UIGraphicsPushContext(context);
[self internalDrawWithRect:self.bounds];
UIGraphicsPopContext();
}
और अपनी कस्टम internalDraw(_: CGRect)विधि बनाएं , साथ ही असफल-सुरक्षित draw(_: CGRect):
तीव्र
/// Internal drawing method; naming's up to you.
func internalDraw(_ rect: CGRect)
{
// @FILLIN: Custom drawing code goes here.
// (Use `UIGraphicsGetCurrentContext()` where necessary.)
}
/// For compatibility, if something besides our display method asks for draw.
override func draw(_ rect: CGRect) {
internalDraw(rect)
}
उद्देश्य सी
/// Internal drawing method; naming's up to you.
- (void)internalDrawWithRect:(CGRect)rect
{
// @FILLIN: Custom drawing code goes here.
// (Use `UIGraphicsGetCurrentContext()` where necessary.)
}
/// For compatibility, if something besides our display method asks for draw.
- (void)drawRect:(CGRect)rect {
[self internalDrawWithRect:rect];
}
और अब बस कॉल करें myView.displayNow()जब भी आपको वास्तव में आकर्षित करने की आवश्यकता हो (जैसे कि CADisplayLinkकॉलबैक से) । हमारे displayNow()विधि बता देंगे CALayerकरने के लिए displayIfNeeded()है, जो तुल्यकालिक हमारे में आपको वापस कॉल करेगा draw(_:,in:)और में ड्राइंग करना internalDraw(_:), क्या पर जाने से पहले संदर्भ में तैयार है के साथ दृश्य को अद्यतन करने।
यह दृष्टिकोण @ RobNapier के उपरोक्त के समान है, लेकिन displayIfNeeded()इसके अतिरिक्त कॉल करने का लाभ है setNeedsDisplay(), जो इसे तुल्यकालिक बनाता है।
यह संभव है क्योंकि CALayers UIViewdo- लेयर्स से अधिक ड्रॉइंग फंक्शनालिटी को उजागर करता है- लेयर्स व्यू की तुलना में लो-लेवल होते हैं और लेआउट के भीतर अत्यधिक-कंफर्टेबल ड्राइंग के उद्देश्य से स्पष्ट रूप से डिजाइन किए जाते हैं, और (Cocoa में कई चीजों की तरह) फ्लेक्सिबल तरीके से डिजाइन किए जाते हैं। एक मूल वर्ग के रूप में, या एक प्रतिनिधि के रूप में, या अन्य ड्राइंग सिस्टम के लिए एक पुल के रूप में, या बस अपने दम पर)। CALayerDelegateप्रोटोकॉल का उचित उपयोग यह सब संभव बनाता है।
कोर एनिमेशन प्रोग्रामिंग गाइडCALayer के सेटिंग अप लेयर ऑब्जेक्ट्स सेक्शन में एस के विन्यास के बारे में अधिक जानकारी पाई जा सकती है ।