Following the case, the case is improved: "iOS Dev 50" uses the creation of new classes to implement code encapsulation, unknowingly practicing a simple MVC experiment, with an animation
In the above case, we finally implemented an animation, click the "Download" button to become "downloaded" is not clickable, and then appear in the middle of the prompt box.
(1) There is a small bug, that is, after the transparency of this hint becomes 0, this box does not appear to remain in memory. Need:
[Tipslabel Removefromsuperview];
(2) Second, we can animate with another code, that is, block, this time is 2 block nesting. Use the following code to implement the cue box gradient and disappear from the removal:
[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) There is another mistake:
[Self.superview.superview Addsubview:tipslabel];
This is actually:
[Self.superview Addsubview:tipslabel];
Because, our self is equivalent to Xib's small figure, it loads in the Viewcontroller view, so its superview is the view controller's view. As for why a Superview no error and normal operation, can think of the answer, is because the above has no superview so no impact.
(4) Enhanced improvement: Turn the cue box into a rounded rectangle and use it to layer layers.
--First, you need to get a layer of the control to make the shape settings.
--and then, what you need to know is that this layer is generally called the primary layer, and we don't usually write it directly on it, but instead load the other sublayers up, but these sublayers are usually shown in their own thoughts. At this point, when our master layer changes, we need to tell all of its sublayers to follow the boundary rules of the master layer, which is what the boundary of my main layer looks like.
The second and third sentences are the same, but one is to set the properties of the control's layer and a property of the control.
tipslabel.layer.cornerradius=15;// Tipslabel.layer.maskstobounds=yes; Tipslabel.clipstobounds=yes;
(5) Enhancements: The following code is a parent-child relationship to find the View Controller view, and then add the control inside, the parent-child relationship lookup is dangerous, such as the day of the parent-child relationship is broken, you need to modify the code.
[Self.superview Addsubview:tipslabel];
We can pass the view controller's view to a parameter, let this parameter to execute.
For example, in XibView.h:
@property (strong,nonatomic) UIView *vcview;
Then in VIEWCONTROLLER.M: (Pass the View controller's view to this parameter)
Xibview.vcview=self.view;
Then, in the XIBVIEW.M:
[Self.vcview Addsubview:tipslabel];
The Self.vcview here is a view of the view controller.
Cons: In this approach, this view is not very independent, and the view controller is too coupled, and when this view controller does not exist, the Vcview view will not work.
Further improvement: with the agent. That is, the class itself sets up a protocol (method) to set the view controller as a proxy, so that when the button is clicked, it notifies the view controller to add the label.
--Set the protocol and proxy properties in XibView.h:
#import <UIKit/UIKit.h> #import "JiuGongGe.h" @class xibview;//need to import this class yourself? Define a protocol @protocol jgxibviewdelegate <NSObject> @optional-(void) XIBVIEWCLICKBTN: (Xibview *) xibview;@ End@interface xibview:uiview//defines a proxy property @property (weak,nonatomic) id<jgxibview@end
-In the XIBVIEW.M:
If the agent has this method, then send a message to the proxy (that is, call this method)
-(Ibaction) Installclick: (UIButton *) btn { //Change button text 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 agent (because the Add label is added in the View controller's view, so the code should be in the view controller, so the view controller is set as the proxy, specifically to perform the add operation):
Abide by the agreement first
@interface Viewcontroller () <JGXibViewDelegate>
Setting up the proxy in Viewdidload
xibview.delegate=self;
Final Implementation method
-(void) XIBVIEWCLICKBTN: (Xibview *) xibview{//Call method to create UILabel UILabel *tipslabel=[xibview Tipsview]; Set 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 Uilabel text and background styles Jiugongge *jiugongge=xibview.jiugongge;//Here is the key, take out the model of this small view that is clicked Tipslabel.text=[nsstring stringwithformat:@ "%@ has been installed successfully! ", jiugongge.name];//take out the model's name Data Tipslabel.textcolor=[uicolor Whitecolor]; Tipslabel.backgroundcolor=[uicolor Graycolor]; Tipslabel.font=[uifont systemfontofsize:14]; tipslabel.layer.cornerradius=15; Tipslabel.layer.maskstobounds=yes; Tipslabel.clipstobounds=yes; Set display time animations, transparent and opaque changes [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];}
"iOS Development-57" case Improvement: Block animation, control Removefromsuperview, use layer to set rounded rectangles, and proxy mode application