2016-08-19 11 views
10

बटन पर क्लिक होने पर हर बार यूआईबटन को 90 डिग्री घुमाएंगे और प्रत्येक घुमावदार स्थिति/कोण का ट्रैक भी रखें?बटन पर क्लिक होने पर हर बार 9 0 डिग्री से यूआईबटन को घुमाने पर

यहाँ कोड मैं अब तक किया है, लेकिन यह केवल एक बार घूमता है:

@IBAction func gameButton(sender: AnyObject) { 
    UIView.animateWithDuration(0.05, animations: ({ 
     self.gameButtonLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) 
    })) 
} 

उत्तर

12
self.gameButtonLabel.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2)) 

CGAffineTransformMake... साथ

// Swift 3 - Rotate the current transform by 90 degrees. 
self.gameButtonLabel.transform = self.gameButtonLabel.transform.rotated(by: CGFloat(M_PI_2)) 

// OR 

// Swift 2.2+ - Pass the current transform into the method so it will rotate it an extra 90 degrees. 
self.gameButtonLabel.transform = CGAffineTransformRotate(self.gameButtonLabel.transform, CGFloat(M_PI_2)) 

इसमें बदला जाना चाहिए, तो आप एक नया बनाने को बदलने और अधिलेखित कोई भी परिवर्तन जो पहले से ही बटन पर था। चूंकि आप ट्रांसफॉर्म के लिए 9 0 डिग्री जोड़ना चाहते हैं जो पहले से मौजूद है (जो 0, 9 0 डिग्री आदि हो सकता है), आपको वर्तमान ट्रांसफॉर्म में जोड़ना होगा। मैंने जो कोड दिया है उसकी दूसरी पंक्ति वह करेगी।

+0

स्विफ्ट 3 संस्करण एक त्रुटि दे: प्रकार के मान 'CGAffineTransform' कोई सदस्य 'घुमाया' और स्विफ्ट 2.2+ संस्करण एक त्रुटि देना है: अतिरिक्त तर्क में कॉल – nodyor90z

+0

हम्म। आप किस एक्सकोड बीटा पर हैं? वे मेरे ऊपर सभी विधि नाम बदलते रहते हैं। मुझे लगता है कि मैंने जो कोड पोस्ट किया है वह एक्सकोड 8 बीटा 5 से है। – keithbhunter

+0

मैंने अभी तक एक्सकोड 8 बीटा में अपग्रेड नहीं किया है। मैं वर्तमान में एक्सकोड 7.3.1 का उपयोग कर रहा हूं लेकिन जब भी मैं अपना कोड लिख रहा हूं तब भी मुझे एक्सकोड से स्विफ्ट 3 ऑटो-सुधार सुझाव मिलते हैं। – nodyor90z

0

स्विफ्ट 4:

@IBOutlet weak var expandButton: UIButton! 

var sectionIsExpanded: Bool = true { 
    didSet { 
     UIView.animate(withDuration: 0.25) { 
      if self.sectionIsExpanded { 
       self.expandButton.transform = CGAffineTransform.identity 
      } else { 
       self.expandButton.transform = CGAffineTransform(rotationAngle: -CGFloat.pi/2.0) 
      } 
     } 
    } 
} 

@IBAction func expandButtonTapped(_ sender: UIButton) { 
    sectionIsExpanded = !sectionIsExpanded 
} 
संबंधित मुद्दे