ios - Add shade gradient to the bottom of a uitableviewcell / uiimageview -


does know how add gradient bottom of uitableviewcell or uiimageview image shown below does?

enter image description here

add class project (swift):

class uigradientimageview: uiimageview {  let mygradientlayer: cagradientlayer   override init?(frame: cgrect){     mygradientlayer = cagradientlayer()     super.init(frame: frame)     self.setup()     addgradientlayer()  }   func addgradientlayer(){     if mygradientlayer.superlayer == nil{         self.layer.addsublayer(mygradientlayer)     }  }   required init(coder adecoder: nscoder){     mygradientlayer = cagradientlayer()     super.init(coder: adecoder)     self.setup()     addgradientlayer()  }   func getcolors() -> [cgcolorref] {     return [uicolor.clearcolor().cgcolor, uicolor(red: 0, green: 0, blue: 0, alpha: 0.5).cgcolor]  }   func getlocations() -> [cgfloat]{     return [0.5,  0.9]  }   func setup() {      mygradientlayer.startpoint = cgpoint(x: 0.5, y: 0)      mygradientlayer.endpoint = cgpoint(x: 0.5, y: 1)       let colors = getcolors()      mygradientlayer.colors = colors      mygradientlayer.opaque = false      mygradientlayer.locations = getlocations()  }   override func layoutsubviews() {     super.layoutsubviews()     mygradientlayer.frame = self.layer.bounds  } } 

update: objective-c translated solution. credits @sleepsonnewspapers.

 #import "uigradientimageview.h"   @interface uigradientimageview()    @property (nonatomic, strong) cagradientlayer *mygradientlayer;   @end   @implementation uigradientimageview   -(instancetype)initwithframe:(cgrect)frame{   if(self){   self = [super initwithframe:frame];   self.mygradientlayer = [[cagradientlayer alloc]init];   [self setup];   [self addgradientlayer];   }  return self;  }   -(instancetype)initwithcoder:(nscoder *)adecoder{   if(self){    self = [super initwithcoder:adecoder];    self.mygradientlayer = [[cagradientlayer alloc]init];    [self setup];    [self addgradientlayer];   }   return self;  }   -(void)addgradientlayer{  if (self.mygradientlayer.superlayer == nil) {   [self.layer addsublayer:self.mygradientlayer];  } } 

make uiimageview class on storyboard 1 instead of default.

enter image description here


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -