ios - How to display more copies of an object? -
objective
i need app display multiple instances of uiimageview. in other words, app should take uiimageview , display more times inside view. new uiimageviews remain identical, position in view changes.
code
to so, firstly declare uiimageview:
let myimage = uiimage(named: "house") let myimageview = uiimageview(image: myimage) myimageview.frame = cgrectmake(view.frame.size.width/2, view.frame.size.height/2, 100, 100) i use for-loop repeat image repetition.
(var = 0; != 10; i++) { // distribuite uiimageviews here self.view.addsubview(myimageview) } though, seem find no way copy uiimageview , display more times. declaring more 1 uiimageview sounds inefficient solution me.
question
is there way display object (uiimageview) multiple times declaring once?
since uiimageview doesn't conform nscopying protocol, have copy yourself: instantiate new uiimageview , copy manually each property think important original new one.
the following example creating 10 new images based on original one. copying uiimage, can complete , copy properties need:
let myimage = uiimage(named: "house") let myimageview = uiimageview(image: myimage) myimageview.frame = cgrectmake(view.frame.size.width/2, view.frame.size.height/2, 100, 100) self.view.addsubview(myimageview) (var = 0; != 10; i++) { var newimageview = uiimageview(image: myimageview.image) newimageview.frame = // yournewframe // copy else properties need original image self.view.addsubview(newimageview) }
Comments
Post a Comment