Use uiscrollview to produce parallax effect
Effect:
Advanced effects:
Source code:
Moreinfoview. H + moreinfoview. m
/// Moreinfoview. h // yxcell // copyright (c) 2014 y. x. all rights reserved. // # import <uikit/uikit. h> @ interface moreinfoview: uiview @ property (nonatomic, strong) uiimageview * imageview; @ end
/// Moreinfoview. M // yxcell // copyright (c) 2014 y. x. all rights reserved. // # import "moreinfoview. H "@ implementation moreinfoview-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {cgrect rect = frame; self. layer. borderwidth = 0.5f; self. layer. bordercolor = [uicolor blackcolor]. cgcolor; self. layer. maskstobounds = yes; _ imageview = [[uiimageview alloc] initwithframe: cgrectmake (-50, 0, rect. size. width + 50*2, rect. size. height)]; [self addsubview: _ imageview];} return self;} @ end
Rootviewcontroller. m
//// Rootviewcontroller. M // yxcell // copyright (c) 2014 y. x. all rights reserved. // # import "rootviewcontroller. H "# import" moreinfoview. H "@ interface rootviewcontroller () <uiscrollviewdelegate> @ property (nonatomic, strong) uiscrollview * scrollview; @ property (nonatomic, assign) cgfloat K; @ property (nonatomic, assign) cgfloat B; @ end @ implementation rootviewcontroller-(void) linearfunctionpointa :( cgpoint) pointa pointb :( cgpoint) pointb {cgfloat X1 = pointa. x; cgfloat Y1 = pointa. y; cgfloat X2 = pointb. x; cgfloat y2 = pointb. y; _ k = calculateslope (x1, Y1, X2, Y2); _ B = calculateconstant (x1, Y1, X2, Y2);}-(void) viewdidload {[Super viewdidload]; self. view. backgroundcolor = [uicolor blackcolor]; // calculate the slope [self linearfunctionpointa: cgpointmake (0,-50) pointb: cgpointmake (320,270)]; _ scrollview = [[uiscrollview alloc] initwithframe: self. view. bounds]; _ scrollview. delegate = self; _ scrollview. pagingenabled = yes; [self. view addsubview: _ scrollview]; nsarray * picarray = @ [[uiimage imagenamed: @ "1"], [uiimage imagenamed: @ "2"], [uiimage imagenamed: @ "3"], [uiimage imagenamed: @ "4"], [uiimage imagenamed: @ "5"]; [picarray enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {moreinfoview * Show = [[moreinfoview alloc] initwithframe: cgrectmake (idx * self. view. bounds. size. width, 0, self. view. bounds. size. width, self. view. bounds. size. height)]; show. imageview. image = OBJ; [_ scrollview addsubview: Show] ;}]; _ scrollview. contentsize = cgsizemake (picarray. count * self. view. bounds. size. width, self. view. bounds. size. height);}-(void) scrollviewdidscroll :( uiscrollview *) scrollview {cgfloat x = scrollview. contentoffset. x; [scrollview. subviews enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {moreinfoview * TMP = OBJ; If ([TMP iskindofclass: [moreinfoview class]) {// produce the parallax effect cgrect rect = TMP. imageview. frame; rect. origin. X = _ K * (X-idx * 320) + _ B; TMP. imageview. frame = rect ;}}] ;}// calculate cgfloat calculateslope (cgfloat X1, cgfloat Y1, cgfloat X2, cgfloat Y2) {return (Y2-Y1) /(x2-X1);} cgfloat calculateconstant (cgfloat X1, cgfloat Y1, cgfloat X2, cgfloat Y2) {return (Y1 * (x2-X1) -X1 * (Y2-Y1)/(x2-X1);} @ end
Note:
1. The value of this place is not set at Will :)
2. Modify the value of 270 to achieve the display effect.
3. The following X-idx * 320 is also critical.