ios - Animate UIImageView Diagonally -
i'd able animate uiimageview background in diagonal fashion. meaning background animate , scroll top left of screen towards bottom right of screen. ideally, continuous animation.
i have directions figured out, however, have seems take copy of background & animate on static background. causes weird effect.
see gif:
here code:
var imageview = uiimageview() override func viewdidload() { super.viewdidload() self.setup(); } func setup() { self.imageview.frame = self.view.bounds self.imageview.image = uiimage(named: "myimage.jpg") self.view.addsubview(self.imageview) self.scroll() } func scroll() { var theimage = uiimage(named: "myimage.jpg")! var pattern = uicolor(patternimage: theimage) var layer = calayer() layer.backgroundcolor = pattern.cgcolor layer.transform = catransform3dmakescale(1,-1,1) layer.anchorpoint = cgpointmake(0, 1) var viewsize = self.imageview.bounds.size layer.frame = cgrectmake(0,0, theimage.size.width + viewsize.width, viewsize.height) self.imageview.layer.addsublayer(layer) var startpoint = cgpointzero var endpoint = cgpointmake(theimage.size.width, theimage.size.height + 15) var animation = cabasicanimation(keypath: "position") animation.timingfunction = camediatimingfunction(name: kcamediatimingfunctionlinear) animation.fromvalue = nsvalue(cgpoint:startpoint) animation.tovalue = nsvalue(cgpoint:endpoint) animation.repeatcount = float.infinity animation.duration = 1.0 layer.addanimation(animation, forkey: "position") }
is able figure out how make whole background scroll? need larger image, , scroll that? if case, contentmode of imageview need set at?
thanks
---- update
i've reworked code bit, , have working version of wanted. i'm curious feedback, i'm still not 100% comfortable animations in general (but i'm learning! :) )
-- removed whole background image in general, duncanc suggested wasn't serving purpose. now, i'm making image layer, trying make if sufficiently big, , looping animation. thoughts?
override func viewdidload() { super.viewdidload() self.scroll() } func scroll() { var theimage = uiimage(named: "myimage.jpg")! var pattern = uicolor(patternimage: theimage) var layer = calayer() layer.backgroundcolor = pattern.cgcolor layer.transform = catransform3dmakescale(1,-1,1) layer.anchorpoint = cgpointmake(0, 1) // i'm making view large here animation can keep running smoothly without // showing behind our image // there better way this? var viewsize = cgsizemake(self.view.bounds.size.height * 10, self.view.bounds.size.width * 10) layer.frame = cgrectmake(0,0, theimage.size.width + viewsize.width, viewsize.height) self.view.layer.addsublayer(layer) var startpoint = cgpointmake(-theimage.size.width, -theimage.size.height) var endpoint = cgpointmake(0, 0) var animation = cabasicanimation(keypath: "position") animation.timingfunction = camediatimingfunction(name: kcamediatimingfunctionlinear) animation.fromvalue = nsvalue(cgpoint:startpoint) animation.tovalue = nsvalue(cgpoint:endpoint) animation.repeatcount = float.infinity animation.duration = 3.0 layer.addanimation(animation, forkey: "position") }
**result:*
your code doesn't make lot of sense. have image view contains image myimage.jpg, , create calayer contains image pattern color. add layer image view , animate it's position.
just use uiview animation , animate center of image view directly. if you're using autolayout you'll need add horizontal , vertical position constraints, wire them iboutlets, , in animation block change constraints , call layoutifneeded()
on image view's superview.
Comments
Post a Comment