As the project began to be more urgent, but also a person, plus Apple automatic layout is more troublesome, measured after the use of frame to layout the screen. Now that you're a little bit idle, start doing some optimizations on the previous code. One of the small features is this, a horizontal sliding scrollview, screen loading from the server to take the background map and text description and jump link, while the picture is cached, the next time the reload if the load fails, loading the previous content. Then this place involves the picture, and the picture caches two parts. This article begins with the screen.
The first time I used automatic layout to write ScrollView, I encountered a pit. Later, I found the answer in a blog. The links are as follows: Uiscrollview add AutoLayout constrained pits, the core meaning is:
1. ScrollView the dimensions of the inner child control cannot be referenced by the size of the ScrollView
2. The constraints of the child controls inside the ScrollView must be complete
design ideas, sub-views ( Advertisementview ) One view, scrolling view ( Advertisementscrollview ) A view and then scroll the view to add a scrollview,viewcontroller inside to add a scrolling view directly.
The code is as follows:
AdvertisementView.h
#import <UIKit/UIKit.h>@interface**Textdetaillabel; -(void) SetTextColor: (Uicolor *) textcolor text: (NSString *) text textdetail: (NSString *) Textdetail; @end
advertisementview.m
#import "AdvertisementView.h"#import "Define.h"@implementationAdvertisementview- (ID) init { self=[Super Init]; if(self) {Self.textlabel=[[UILabel alloc] init]; [Self.textlabel setfont:[uifont fontwithname:font_name Size: the]]; [Self.textlabel Settextalignment:nstextalignmentright]; [Self AddSubview:self.textLabel]; Self.textdetaillabel=[[UILabel alloc] init]; [Self.textdetaillabel setfont:[uifont fontwithname:font_name Size: -]]; [Self.textdetaillabel Settextalignment:nstextalignmentright]; [Self AddSubview:self.textDetailLabel]; } returnSelf ;}- (void) layoutsubviews {[Self.textdetaillabel mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.equalTo (self). With.offset (0); Make.right.equalTo (self). With.offset (- the); Make.bottom.equalTo (self). With.offset (- the); Make.height.mas_equalTo (@ -); }]; [Self.textlabel mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.equalTo (self). With.offset (0); Make.right.equalTo (self). With.offset (- the); Make.bottom.equalTo (self.textDetailLabel.mas_top). With.offset (-5); Make.height.mas_equalTo (@ the); }];}#pragmaMark-load data-(void) SetTextColor: (Uicolor *) textcolor text: (NSString *) text textdetail: (NSString *) Textdetail {if(self) {[Self.textlabel settext:text]; [Self.textlabel Settextcolor:textcolor]; [Self.textdetaillabel Settext:textdetail]; [Self.textdetaillabel Settextcolor:textcolor]; } Else{NSLog (@"Error:advertisementview class is not init successfully."); }}@end
AdvertisementScrollView.h
#import <UIKit/UIKit.h>#import"AdvertisementView.h"@ Interface Advertisementscrollview:uiview <UIScrollViewDelegate> { ** Viewarray; @end
advertisementscrollview.m
#import "AdvertisementScrollView.h"@interfaceAdvertisementscrollview () @property (nonatomic, strong) Uiscrollview*ScrollView, @property (nonatomic, strong) UIView*Contentview, @property (nonatomic, strong) Uipagecontrol*Pagecontrol;@end@implementationAdvertisementscrollview- (ID) init { self=[Super Init]; if(self) {[self addSubview:self.scrollView]; } returnSelf ;}- (void) layoutsubviews {[Super layoutsubviews]; [Self.scrollview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.edges.equalTo (self); }]; [Self.pagecontrol mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.bottom.mas_equalTo (Self.mas_bottom); Make.centerX.equalTo (Self.mas_centerx); Make.size.mas_equalTo (Cgsizemake (Self.bounds.size.width/2, -)); }]; [Self.contentview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.edges.equalTo (Self.scrollview); Make.height.equalTo (Self.scrollview); }]; Advertisementview*lastadview =Nil; for(Advertisementview *adviewinchself.viewarray) {[AdView mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.equalTo (Lastadview? Lastadview.mas_right: @0); Make.bottom.equalTo (@0); Make.width.equalTo (Self.scrollView.mas_width); Make.height.equalTo (Self.scrollView.mas_height); }]; Lastadview=AdView; } [Self.contentview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.right.equalTo (lastadview.mas_right); }];}#pragmamark-uiscrollviewdelegate-(void) Scrollviewdidscroll: (Uiscrollview *) ScrollView {cgfloat pagewidth=ScrollView.frame.size.width; intCurrentPage = Floor ((Scrollview.contentoffset.x-pagewidth/2)/PageWidth) +1; Self.pageControl.currentPage=currentpage;}#pragmamark-getters&setters-(Uiscrollview *) ScrollView {if(_scrollview = =Nil) {_scrollview=[[Uiscrollview alloc] init]; _scrollview.backgroundcolor=[Uicolor Redcolor]; _scrollview.Delegate=Self ; _scrollview.bounces=NO; _scrollview.pagingenabled=YES; _scrollview.showsverticalscrollindicator=NO; _scrollview.showshorizontalscrollindicator=NO; [_scrollview AddSubview:self.contentView]; } return_scrollview;}-(UIView *) Contentview {if(_contentview = =Nil) {_contentview=[[UIView alloc] init]; } return_contentview;}-(Uipagecontrol *) Pagecontrol {if(_pagecontrol = =Nil) {_pagecontrol=[[Uipagecontrol alloc] init]; [_pagecontrol Setcurrentpageindicatortintcolor:[uicolor Whitecolor]; [_pagecontrol Setpageindicatortintcolor:[uicolor Graycolor]; [_pagecontrol setcurrentpage:0]; [_pagecontrol Setnumberofpages:[self.viewarray Count]]; } return_pagecontrol;}-(Nsarray *) Viewarray {return_viewarray;}- (void) Setviewarray: (Nsarray *) Viewarray {_viewarray=Viewarray; for(Advertisementview *adviewinchself.viewarray) {[Self.contentview Addsubview:adview]; } [self AddSubview:self.pageControl];}@end
MainPersonalInfoViewController.h
#import <UIKit/UIKit.h>#import"AdvertisementScrollView.h" #import " AdvertisementView.h " @interface Mainpersonalinfoviewcontroller:uiviewcontroller { ** *Adscrollview; @end
Mainpersonalinfoviewcontroller.m
#import "MainPersonalInfoViewController.h"@interfaceMainpersonalinfoviewcontroller ()@end@implementationMainpersonalinfoviewcontroller- (void) viewdidload {[Super viewdidload]; [Self.view AddSubview:self.adScrollView];}- (void) Viewwillappear: (BOOL) animated {[Super viewwillappear:animated]; [Self.adscrollview mas_makeconstraints:^ (Masconstraintmaker *Make ) {Make.left.equalTo (Self.view). With.offset (0); Make.right.equalTo (Self.view). With.offset (0); Make.top.equalTo (Self.view). With.offset (0); Make.height.mas_equalTo (@ Max); }];}- (void) didreceivememorywarning {[Super didreceivememorywarning];}#pragmamark-getters&setters-(Advertisementscrollview *) Adscrollview {if(_adscrollview = =Nil) {_adscrollview=[[Advertisementscrollview alloc] init]; [_adscrollview Setbackgroundcolor:[uicolor Graycolor]; Advertisementview*adview1 =[[Advertisementview alloc] init]; [AdView1 setbackgroundcolor:[self Randomcolor]; [AdView1 settextcolor:[self Randomcolor] Text:@"What about 2.0T four cylinders? "Textdetail:@"test Audi Q7 40TFSI S Line"]; Advertisementview*adview2 =[[Advertisementview alloc] init]; [AdView2 setbackgroundcolor:[self Randomcolor]; [AdView2 settextcolor:[self Randomcolor] Text:@"interpreting low-cost vehicles"Textdetail:@"Real clap Nissan 1.2T model"]; [_adscrollview Setviewarray:[@[adview1, AdView2] mutablecopy]; } return_adscrollview;}-(Uicolor *) Randomcolor {cgfloat hue= (Arc4random ()% the/256.0);//0.0 to 1.0CGFloat saturation = (arc4random ()% -/256.0) +0.5;//0.5 to 1.0, awayCGFloat brightness = (arc4random ()% -/256.0) +0.5;//0.5 to 1.0, away from black return[Uicolor Colorwithhue:hue saturation:saturation brightness:brightness Alpha:1];}@end
Above.
Uiscrollview Automatic layout of masonry