ios - Load imageView from Xcassets using Switch statement -
i have images @1 @2 , @3 in xcassets , trying load image onto scrollview page using code below switches image due age on. page determined position , switch statement called in viewwillload function. images not loading sounds working know image loading problem. can help?
override func viewdiddisappear(animated: bool) { self.imageview.removefromsuperview() self.imageview.image = nil } override func viewwillappear(animated: bool) { showimageview() let tapgestureimage = uitapgesturerecognizer(target: self, action: selector("handletapgestureimage:")) imageview.addgesturerecognizer(tapgestureimage) imageview.userinteractionenabled = true } // mark: bwwalkthroughpage implementation func walkthroughdidscroll(position: cgfloat, offset: cgfloat) { // getting page number scroll position. first page loads nil want zero. screenpage = int(((position / view.bounds.size.width) + 1) - 1) } func showimageview() { // change imageview in bundle depending on scrollview page are. switch screenpage { case 0: self.imageview.image = uiimage(named: "aligator", inbundle: nsbundle.mainbundle(), compatiblewithtraitcollection: self.traitcollection) self.view.addsubview(imageview) case 1: self.imageview.image = uiimage(named: "bear", inbundle: nsbundle.mainbundle(), compatiblewithtraitcollection: self.traitcollection) self.view.addsubview(imageview) default: self.imageview.image = nil } }
a few things important understand. not need add/remove image view view hierarchy on , over. calling imageview.image = nil
suffice.
the second thing don't need use full method uiimage(named:inbundle:compatiblewithtraitcollection:)
. should able use uiimage(named:)
. if find having use former, may taking bigger bites can handle @ stage in career.
the third thing when override method, must call super in 99% of cases.
here's example should work:
import uikit class myviewcontroller: uiviewcontroller { override fun viewdidload() { super.viewdidload() let gesturerecognizer = uitapgesturerecognizer(target: self, action: selector("handletapgestureimage:")) imageview.addgesturerecognizer(gesturerecognizer) imageview.userinteractionenabled = true } override func viewdiddisappear(animated: bool) { super.viewdiddisappear(animated: animated) self.imageview.image = nil } override func viewwillappear(animated: bool) { super.viewwillappear(animated: animated) showimageview() } func walkthroughdidscroll(position: cgfloat, offset: cgfloat) { screenpage = int(((position / view.bounds.size.width) + 1) - 1) } func showimageview() { switch screenpage { case 0: imageview.image = uiimage(named: "aligator") case 1: imageview.image = uiimage(named: "bear") default: imageview.image = nil } } }
Comments
Post a Comment