Make a uiview instead of the Viewcontroller navigation bar view to follow the ScrollView slide and change the size and the picture to move the interlaced effect, the list of the header view of the picture produced Parallax scrolling effect
Source: Internet
Author: User
<span id="Label3"></p><p><p>#import <UIKit/UIKit.h></p></p><p><p>@interface Elheaderview:uiview<br>@property (nonatomic, weak) Uiviewcontroller *viewcontroller;<br>@property (nonatomic, weak) Uiscrollview *scrollview;</p></p><p><p>-(id) initwithframe: (cgrect) frame backgroudimageurl: (nsstring *) backimageurl headerimageurl: (nsstring *) Headerimageurl title: (nsstring *) title subTitle: (nsstring *) subTitle;<br>-(void) updatesubviewswithscrolloffset: (cgpoint) newoffset;<br>@end</p></p><p>#import "ELHeaderView.h"<br>#import "uiimageview+webcache.h"<br>#pragma mark-notes:<br>/*<br>Self<br>UIView<br>Self.clipstobounds = Yes;//this is Important.<br>Rewrite-(void) willmovetosuperview: (UIView *) The Newsuperview method sets some view objects (this method executes when the object is loaded):<br>1. Hide the navigation bar of the Viewcontroler member object that you are adding to:<br>[[self.viewcontroller navigationcontroller] setnavigationbarhidden:yes];<br>2. Let yourself be the observer of your own ScrollView member variable (KVO) observing its contentoffset:<br>[self.scrollview addobserver:self forkeypath:@ "contentoffset" options: (nskeyvalueobservingoptionnew) context:Nil];<br>It is therefore necessary to implement the KVO Observer callback to get the contentoffset of the new scrollerview:<br>-(void) observevalueforkeypath: (nsstring *) keypath ofobject: (id) object change: (nsdictionary *) change context: (void *) Context<br>And calling a custom method in the callback calculates the position of the individual view and the fade and font size:<br>-(void) updatesubviewswithscrolloffset: (cgpoint) newoffset;<br>3. Set the interval at the top of the scrollview://this is Important.<br>Self.scrollView.contentInset = Uiedgeinsetsmake (self.frame.size.height, 0, 0, 0);<br> <br> <br> <br>Scrollerview:<br>Added to the Viewcontroller:<br>Background map:<br>Attached to Self.<br>Y Is-self.frame.size.height/2<br>Gao is self.frame.size.height*1.5<br> <br>Table Header Picture:<br>Attached to Self.<br>Z-axis on the background map<br>resizing, position<br>Table Header main Title:<br>Attached to Self.<br>Z-axis on the background map<br>resizing, position<br>Table Header Subtitle:<br>Attached to Self.<br>Z-axis on the background map<br>resizing, position<br>*/</p><p><p>@interface Elheaderview ()<br>@property (nonatomic, Strong) Uiimageview *backimageview;<br>@property (nonatomic, Strong) Uiimageview *headerimageview;<br>@property (nonatomic, Strong) UILabel *titlelabel;<br>@property (nonatomic,strong) UILabel *subtitlelabel;<br>@end</p></p><p><p></p></p><p><p>@implementation Elheaderview</p></p><p><br>-(id) initwithframe: (cgrect) frame backgroudimageurl: (nsstring *) backimageurl headerimageurl: (nsstring *) Headerimageurl title: (nsstring *) title subTitle: (nsstring *) subtitle{<br>Self = [super initwithframe:frame];<br>If (self) {<br>Self.backgroundcolor = [uicolor whitecolor];<br>_backimageview = [[uiimageview alloc] initwithframe:cgrectmake (0, -0.5*frame.size.height, frame.size.width, frame.size.height*1.5)];<br>[_backimageview Setimagewithurl:[nsurl urlwithstring:backimageurl];<br>_headerimageview = [[uiimageview alloc] initwithframe:cgrectmake (frame.size.width*0.5-0.125*frame.size.height, 0.25 *frame.size.height, 0.25*frame.size.height, 0.25*frame.size.height)];<br>[_headerimageview Setimagewithurl:[nsurl urlwithstring:headerimageurl];<br>_titlelabel = [[UILabel alloc] initwithframe:cgrectmake (0, 0.6*frame.size.height, frame.size.width, frame.size.height *0.2)];<br>_titlelabel.textalignment = nstextalignmentcenter;<br>_titlelabel.font = [uifont boldsystemfontofsize:20];<br>_titlelabel.text = title;<br>_subtitlelabel = [[UILabel alloc] initwithframe:cgrectmake (0, 0.85*frame.size.height, frame.size.width, frame.size.height*0.1)];<br>_subtitlelabel.textalignment = nstextalignmentcenter;<br>_subtitlelabel.font = [uifont systemfontofsize:12];<br>_subtitlelabel.text = subTitle;<br>_titlelabel.textcolor = [uicolor whitecolor];<br>_subtitlelabel.textcolor = [uicolor whitecolor];<br> <br>[self addsubview:_backimageview];<br>[self addsubview:_headerimageview];<br>[self addsubview:_titlelabel];<br>[self addsubview:_subtitlelabel];<br>Self.clipstobounds = YES;<br>}<br>Return self;<br>}</p><p><p>-(void) willmovetosuperview: (UIView *) newsuperview{<br>[[self.viewcontroller navigationcontroller] setnavigationbarhidden:yes];<br>[self.scrollview addobserver:self forkeypath:@ "contentoffset" options: (nskeyvalueobservingoptionnew) context:Nil];<br>Self.scrollView.contentInset = Uiedgeinsetsmake (self.frame.size.height, 0, 0, 0);<br>}</p></p><p><p>-(void) observevalueforkeypath: (nsstring *) keypath ofobject: (id) object change: (nsdictionary *) change context: (void *) context{<br>Cgpoint newoffset = [change[@ "new"] cgpointvalue];<br>[self updatesubviewswithscrolloffset:newoffset];<br>}</p></p><p>-(void) updatesubviewswithscrolloffset: (cgpoint) newoffset{<br>Float Destinaoffset =-60;<br>Float Startchangeoffset =-self.scrollview.contentinset.top;<br>Newoffset = Cgpointmake (newoffset.x, newoffset.y<startchangeoffset?startchangeoffset: (newOffset.y> destinaoffset?destinaoffset:newoffset.y));<br>float Titledestinateoffset = self.frame.size.height-40;<br>Float Newy =-newoffset.y-self.scrollview.contentinset.top;<br>Float d = destinaoffset-startchangeoffset;<br>float alpha = n (newoffset.y-startchangeoffset)/d;<br>Self.subTitleLabel.alpha = alpha;<br>Self.frame = CGRectMake (0, newy, self.frame.size.width, self.frame.size.height);<br>Self.backImageView.frame = CGRectMake (0, -0.5*self.frame.size.height+ (1.5*self.frame.size.height-60) * (1-alpha), self.backImageView.frame.size.width, self.backImageView.frame.size.height);<br>Self.titleLabel.frame = CGRectMake (0, 0.6*self.frame.size.height+ (titledestinateoffset-0.6*self.frame.size.height ) * (1-alpha), self.titleLabel.frame.size.width, self.titleLabel.frame.size.height);<br>Self.titleLabel.font = [uifont boldsystemfontofsize:16+ (alpha)];<br>}<br>@end</p><p><p>Make a uiview instead of the Viewcontroller navigation bar view to follow the ScrollView slide and change the size and the picture to move the interlaced effect, the list of the header view of the picture produced Parallax scrolling effect</p></p></span>
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