Ios two subviews enter and exit at the same time

Source: Internet
Author: User

Two views targetSubview and sourceSubview are added to a view controller. TargetSubview is hidden by default, and sourceSubview is visible by default.

Now, I have such a requirement. Slide your finger and drop the targetSubview from the top down to the screen. At the same time, the sourceSubview will exit from the bottom of the screen. Synchronous entry and exit of two views is a synchronous operation, which is implemented by animation.

The first method is implemented by using the class method transition OF UIView,

[UIView transitionFromView: sourceSubview toView: targetSubview duration: 1.0 options: UIViewAnimationOptionTransitionFlipFromLeft completion: nil];

However, you can only flip up and down or left and right, but you cannot find the effect of slide in and out. In addition, this method also removes sourceSubview.

Therefore, this method cannot meet the requirements.

The second method is to use the class method of UIView to implement Animations. It is to use delegate to perform some pre-or post-operations.

TargetSubview. frame = CGRectMake (0, targetSubview. frame. size. height + 50, targetSubview. frame. size. width, targetSubview. frame. size. height );

[UIView beginAnimations: @ "Anim1" context: nil];

[UIView setAnimationDuration: 1];

[UIView setAnimationCurve: UIViewAnimationCurveEaseIn];

[UIView setAnimationDelegate: self];

TargetSubview. hidden = NO;

TargetSubview. frame = CGRectMake (0, 0, targetSubview. frame. size. width, targetSubview. frame. size. height );

[UIView commitAnimations];

[UIView beginAnimations: @ "Anim2" context: nil];

[UIView setAnimationDuration: 3];

[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];

[UIView setAnimationDelegate: self];

SourceSubview. frame = CGRectMake (0,-50, sourceSubview. frame. size. width, sourceSubview. frame. size. height );

SourceSubview. hidden = YES;

[UIView commitAnimations];

// Execute after the animation ends

// SourceSubview. hidden = YES;

This method allows the targetSubview to enter slowly, but the sourceSubview disappears immediately without any animation effect.

Therefore, in the second animation setting, I set sourceSubview. hidden to yes. If it is not set, the animation effect will occur, but the sourceSubview will be left on the screen because it is too high.

This attribute cannot be set even after the animation ends.

The effect of this animation is actually executed in the background, similar to that of multithreading. The animation running time is set to 1 second, but the program will not wait. Therefore, you need to set delegate to achieve this effect. After the animation ends, the delegate method + (void) setAnimationDidStopSelector :( SEL) selector must be implemented and sourceSubview. hidden = YES; can be called in this method.

Before the animation starts, there is also a delegate method + (void) setAnimationWillStartSelector :( SEL) selector, which can be set.

The third method is to use the class method Animations of UIView to implement it, but it is implemented by block and can be executed only after ios4.0 or later. Now Apple has upgraded all ios versions to version 7.0, and fewer old versions are available.

TargetSubview. alpha = 0.0;

TargetSubview. hidden = NO;

[UIView animateWithDuration: 2 delay: 0 options: UIViewAnimationOptionCurveLinear animations: ^ {

TargetSubview. frame = CGRectMake (0,-50, targetSubview. frame. size. width, targetSubview. frame. size. height );

TargetSubview. alpha = 1.0;

TargetSubview. frame = CGRectMake (0, 0, targetSubview. frame. size. width, targetSubview. frame. size. height );

SourceSubview. frame = CGRectMake (0, sourceSubview. frame. size. height, sourceSubview. frame. size. width, sourceSubview. frame. size. height );

SourceSubview. alpha = 0.0;

} Completion: ^ (BOOL finished ){

[Self printSubviews];

SourceSubview. hidden = YES;

SourceSubview. alpha = 1.0;

}];

Finally, this simple method is used to meet the requirements.

After the animation ends, set sourceSubview to hidden.

You can set the animation effect attributes in UIView:
Frame
Bounds
Center
Transform
Alpha
BackgroundColor
ContentStretch

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.