2015-04-14 6 views
7

मैंका उपयोग कर रहा हूं ताकि UIButton का शीर्षक प्रत्येक सेकेंड को अपडेट किया जा सके।UIButton के सेटटाइटल को रोकें: forState: एनीमेशन

यह काम करता है लेकिन शीर्षक में टेक्स्ट ब्लिंक (अल्फा 0 और पीछे) स्वचालित रूप से एनिमेट करता है।

मैंने button.layer.removeAllAnimations() का उपयोग किसी भाग्य के साथ करने की कोशिश नहीं की, और कोई अपवाद नहीं है, इसलिए क्वार्ट्जकोर सही ढंग से जुड़ा हुआ प्रतीत होता है।


वर्तमान गैर काम पागल कोड:

UIView.setAnimationsEnabled(false) 
UIView.performWithoutAnimation { 
    button.setTitle(time, forState: .Normal) 
    button.layer.removeAllAnimations() 
     } 
UIView.setAnimationsEnabled(true) 
+0

क्या आपने इसे 'UIView' के' प्रदर्शन के साथ लपेटने का प्रयास किया था। _ एनीमेशन के बिना एनीमेशन:() -> शून्य) '(आईओएस> = 7)? या 'setAnimationsEnabled (_ सक्षम: बूल) '? – robertvojta

उत्तर

29

सुनिश्चित करें कि आपका बटन "कस्टम" बटन है और "सिस्टम" बटन नहीं है।

यदि आपने इसे स्टोरीबोर्ड पर बनाया है, तो बस बटन प्रकार बदलें। यदि आप इसे प्रोग्रामेटिक रूप से बनाने, तो यह होना चाहिए:

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
+1

अच्छा! फिर भी मुझे समझ में नहीं आ रहा है कि एक सिस्टम बटन मानक UIKit आदेशों का पालन करने से इनकार क्यों करता है। – Rivera

4

आप बंद अंदर बटन परिवर्तन कर सकते हैं:

UIView.performWithoutAnimation { 
    //Button changes 
    button.layoutIfNeeded() 
} 

आशा इस मदद करता है।

+0

बहुत बुरा, एक ही परिणाम। – Rivera

+0

इसके अलावा, पर कॉल करने के लिए यह छोटा है UIView.performWithoutAnimation {// बटन परिवर्तन} आपको अभी भी लेआउट कॉल करने की आवश्यकता है IFNeeded() – varu

13

मैं यह करने के लिए एक स्विफ्ट विस्तार कर दिया है:

extension UIButton { 
    func setTitleWithOutAnimation(title: String?) { 
     UIView.setAnimationsEnabled(false) 

     setTitle(title, forState: .Normal) 

     layoutIfNeeded() 
     UIView.setAnimationsEnabled(true) 
    } 
} 

आईओएस 8 और 9 पर मेरे लिए काम करता है, UIButtonTypeSystem साथ।

+1

नवीनतम स्विफ्ट रिलीज के साथ भी मेरे लिए काम करता है। आईबी में UIButtonTypeSystem का भी उपयोग करना। धन्यवाद! – ObjectiveTC

+0

स्विफ्ट 3.1 पर परीक्षण किया गया, अभी भी काम करता है! – PaulRBerg

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