2012-04-20 19 views
11

के साथ UIView के आकार को आसानी से घुमाएं और बदलें। मेरे पास एक छाया और UIImageView सबव्यू के साथ UIView है।छाया

आईपैड घुमाए जाने पर मैं दृश्य का आकार बदलना चाहता हूं और मैं इसे willRotateToInterfaceOrientation कॉलबैक में करने की कोशिश कर रहा हूं।

यदि मैंने मूल तरीके से UIView पर छाया सेट की है तो घूर्णन बहुत चंचल है; तो मुझे छाया सेटिंग परत सेट करने के तरीके पर दूसरों से कुछ सुझाव चाहिए। छायापाथ।

मैंने [UIView animateWithDuration:animations] का उपयोग करके फ्रेम आकार परिवर्तन को एनिमेट करने और उसी ब्लॉक में नया shadowPath सेट करने का प्रयास किया है, लेकिन छाया पथ नए आकार में आ गया है।

और यदि मैं एनिमेशन ब्लॉक में परत की छायापाथ नहीं बदलता, तो यह नहीं बदलता है।

कुछ खोजों से मैंने परत गुणों में परिवर्तन को एनिमेट करने की आवश्यकता CABasicAnimation के साथ की जानी चाहिए।

तो मुझे लगता है कि सवाल यह हो सकता है कि "मैं UIView के फ्रेम आकार और परत परिवर्तन को एक साथ कैसे एनिमेट कर सकता हूं?"

उत्तर

8

एक उम्मीद से थोड़ा अधिक कोड है लेकिन ऐसा कुछ काम करना चाहिए।

CGFloat animationDuration = 5.0; 

    // Create the CABasicAnimation for the shadow 
    CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 
    shadowAnimation.duration = animationDuration; 
    shadowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // Match the easing of the UIView block animation 
    shadowAnimation.fromValue = (id)self.shadowedView.layer.shadowPath; 

    // Animate the frame change on the view 
    [UIView animateWithDuration:animationDuration 
         delay:0.0f 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
        self.shadowedView.frame = CGRectMake(self.shadowedView.frame.origin.x, 
                  self.shadowedView.frame.origin.y, 
                  self.shadowedView.frame.size.width * 2., 
                  self.shadowedView.frame.size.height * 2); 
        } completion:nil]; 

    // Set the toValue for the animation to the new frame of the view 
    shadowAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:self.shadowedView.bounds].CGPath; 

    // Add the shadow path animation 
    [self.shadowedView.layer addAnimation:shadowAnimation forKey:@"shadowPath"]; 

    // Set the new shadow path 
    self.shadowedView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.shadowedView.bounds].CGPath; 
संबंधित मुद्दे