Swift-Using Uiscrollview to achieve parallax animation effect
Effect
Source
Https://github.com/YouXianMing/Swift-Animations
////Moreinfoview.swift//swift-animations////Created by youxianming on 16/8/18.//copyright©2016 year youxianming. All rights reserved.//Import UIKitclassMoreinfoview:uiview {var imageview:uiimageview!OverrideInit (frame:cgrect) {super.init (frame:frame) layer.borderwidth=0.5Layer.bordercolor=Uicolor.blackcolor (). Cgcolor Layer.maskstobounds=true /**--------------* *-50->|-view-width-|<-50-* *--------------* */Let rect=Frame ImageView= Uiimageview (Frame:cgrectmake (- -,0, Rect.size.width + -*2, Rect.size.height)) Imageview.contentmode= . Scaleaspectfill Self.addsubview (ImageView)} required init?(coder Adecoder:nscoder) {fatalerror ("Init (coder:) has not been implemented") }}
////Scrollimageviewcontroller.swift//swift-animations////Created by youxianming on 16/8/18.//copyright©2016 year youxianming. All rights reserved.//Import UIKitclassScrollimageviewcontroller:fulltitlevisualeffectviewcontroller, uiscrollviewdelegate {let viewtag:in T! = +var oncelinearequation:math!var pictures: [UIImage]!var scrollview:uiscrollview!Overridefunc Setup () {super.setup () oncelinearequation= Math (Pointa:cgpointmake (0, - -), Pointb:cgpointmake (contentview!. Width the- the)) Pictures=[UIImage] () pictures.append (UIImage (named:"1")!) Pictures.append (UIImage (named:"2")!) Pictures.append (UIImage (named:"3")!) Pictures.append (UIImage (named:"4")!) Pictures.append (UIImage (named:"5")!) ScrollView= Uiscrollview (frame: (Contentview?. Bounds)!) ScrollView.Delegate=Self scrollview.pagingenabled=trueScrollview.backgroundcolor=Uicolor.blackcolor () scrollview.showshorizontalscrollindicator=falsescrollview.bounces=falsescrollview.contentsize= Cgsizemake (CGFloat (pictures.count) *width, height) contentview?. Addsubview (ScrollView) forIinch 0.. <Pictures.count {Let ShowView= Moreinfoview (Frame:cgrectmake (CGFloat (i) * width,0, width, height)) ShowView.imageView.image=Pictures[i] Showview.tag= Viewtag +i Scrollview.addsubview (ShowView)}} Func Scrollviewdidscroll (scrollview:uiscrollview) {Let X=Scrollview.contentoffset.x forIinch 0.. <Pictures.count {Let ShowView= Scrollview.viewwithtag (Viewtag + i) as!Moreinfoview showview.imageview.x= ONCELINEAREQUATION.K * (x-cgfloat (i) * width) +oncelinearequation.b} }}
Swift-Using Uiscrollview to achieve parallax animation effect