[IOS development-57] case improvement: block animation, removeFromSuperview of controls, use layer to set rounded rectangle and apply proxy mode,

Source: Internet
Author: User

[IOS development-57] case improvement: block animation, removeFromSuperview of controls, use layer to set rounded rectangle and apply proxy mode,

Based on the above case, the case is improved: [iOS development-50] uses the creation of new classes to implement code encapsulation, so that a simple MVC experiment can be practiced without knowing it, with an animation attached


In the above case, we finally implemented an animation. clicking the "Download" button becomes "downloaded" cannot be clicked, and a prompt box appears in the middle.


(1) There is a small BUG, that is, after the transparency of the prompt is changed to 0, the prompt box is not displayed and remains in the memory. Required:

[tipsLabel removeFromSuperview];

(2) second, we can use another code to implement animation, that is, using blocks. This time, two blocks are nested. Use the following code to implement the gradient of the prompt box and remove it:

 [UIView animateWithDuration:2.5 animations:^{        tipsLabel.alpha=1;    } completion:^(BOOL finished) {        [UIView animateWithDuration:2.5 animations:^{           tipsLabel.alpha=0;        } completion:^(BOOL finished) {            [tipsLabel removeFromSuperview];        }];    }];

(3) Another mistake is:

[self.superview.superview addSubview:tipsLabel];

Here is actually:

[self.superview addSubview:tipsLabel];

Because self is equivalent to a small xib image, it is loaded into the ViewController view, so its superview is the view of the View Controller. As to why a superview is not reported and still works properly, the answer is that there is no superview above, so there is no impact.


(4) enhancement: converts the prompt box into a rounded rectangle and uses the layer.

-- First, you need to get a control layer for shape setting.

-- Next, we need to know that this layer is generally called a primary layer. We generally load other child layers instead of writing something on it, however, these child layers are generally displayed according to their own ideas. At this time, when the main layer changes, we need to tell all its child layers to follow the border rules of the main layer, that is, what the main layer looks like and what the border you will look like.

-- The second and third sentences mean the same, but one is to set the properties of the control layer and the other is to set the properties of the control.

    tipsLabel.layer.cornerRadius=15;//    tipsLabel.layer.masksToBounds=YES;    tipsLabel.clipsToBounds=YES;

(5) enhancement: the following code finds the View Controller view through the parent-child relationship, and then adds controls to it. It is dangerous to search for this parent-child relationship, for example, if the parent-child relationship breaks down, you need to modify the code.

[self.superview addSubview:tipsLabel];


We can pass the View Controller's view to a parameter for execution.

For example, in xibView. h:

@property (strong,nonatomic) UIView *vcView;

Then in ViewController. m: (pass the view of the View Controller to this parameter)

xibView.vcView=self.view;

Then, in xibView. m:

[self.vcView addSubview:tipsLabel];

Here, self. vcView is the view of the View Controller.

Disadvantage: In this method, this view is not very independent and has a strong coupling with this view controller. When this view controller does not exist, this vcView view cannot work.

Further improvement: Use a proxy. This class sets a protocol (method) and sets the View Controller as a proxy. When you click the button, the view controller is notified to add the label.

-- SET protocol and proxy attributes in XibView. h:

# Import <UIKit/UIKit. h> # import "JiuGongGe. h" @ class XibView; // do you need to import your own class? // Define a protocol @ protocol JGXibViewDelegate <NSObject> @ optional-(void) xibViewClickBtn :( XibView *) xibView; @ end @ interface XibView: UIView // define a proxy property @ property (weak, nonatomic) id <JGXibView @ end


-- In XibView. m:

If the proxy has this method, send a message to the proxy (that is, call this method)

-(IBAction) installClick :( UIButton *) btn {// the text of the change button has been set to disable [btn setTitle: @ "installed" forState: UIControlStateDisabled]; btn. enabled = NO; if ([self. delegate respondsToSelector: @ selector (xibViewClickBtn :)]) {[self. delegate xibViewClickBtn: self];}


-- In the proxy (because the label is added to the View Controller, the code should be in the View Controller, so set the View Controller as the proxy and perform the add operation):

First observe the agreement

@interface ViewController ()<JGXibViewDelegate>

Set proxy in viewDidLoad

xibView.delegate=self;

Final Implementation Method

-(Void) xibViewClickBtn :( XibView *) xibView {// call the method to create UILabel * tipsLabel = [XibView tipsView]; // set the UILabel display position CGFloat tipW = 250; CGFloat tipH = 30; tipsLabel. frame = CGRectMake (self. view. frame. size. width-tipW)/2, self. view. frame. size. height/2, tipW, tipH); // set the UILabel text and background style JiuGongGe * jiugongge = xibView. jiuGongGe; // the key here is to take the model of the clicked small view out of tipsLabel. text = [NSString stringWithFormat: @ "% @ installed successfully! ", Jiugongge. name]; // retrieves the name data tipsLabel of this model. textColor = [UIColor whiteColor]; tipsLabel. backgroundColor = [UIColor grayColor]; tipsLabel. font = [UIFont systemFontOfSize: 14]; tipsLabel. layer. cornerRadius = 15; // tipsLabel. layer. masksToBounds = YES; tipsLabel. clipsToBounds = YES; // set the animation to be displayed, transparency and opacity. [UIView animateWithDuration: 2.5 animations: ^ {tipsLabel. alpha = 1;} completion: ^ (BOOL finished) {[UIView animateWithDuration: 2.5 animations: ^ {tipsLabel. alpha = 0 ;}completion: ^ (BOOL finished) {[tipsLabel removeFromSuperview] ;}]; [self. view addSubview: tipsLabel];}




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.