ios - Adding an action to a custom button -
i have custom button animation , inside view. i'm having problems adding action button. here code. logout
button , don't know how add action on log user out.
class exampleviewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() self.view.backgroundcolor = uicolor.white self.addcircleview() // additional setup after loading view, typically nib. } func addcircleview() { var circleview1 = vmbuttoncirclefun(frame: cgrect(x: cgfloat(0), y: cgfloat(0), width: cgfloat(60), height: cgfloat(60))) circleview1.addcirclelayer(withtype: vmmakelocation.top.rawvalue) circleview1.strokecolor = uicolor(red: 243/255, green: 106/255, blue: 106/255, alpha: 1.0) circleview1.center = cgpoint(x: cgfloat(self.view.bounds.width / 2 - 100), y: cgfloat(self.view.bounds.height / 2)) circleview1.seticonbutton(uiimage(named: "layer 14.png")!, withtype: vmmakelocation.top.rawvalue, with: uicolor(red: 127/255, green: 140/255, blue: 141/255, alpha: 1.0)) circleview1.setlinewidthvalue(1.0) self.view.addsubview(circleview1) circleview1.buildbutton() circleview1.addtarget(self, action: #selector(self.logout), forcontrolevents: .touchupinside) } func logout() { //todo make logout stuffs }
replace code in vmbuttoncirclefun.h, , vmbuttoncirclefun.m 1 , check code updated of uiviewcontroller,
the problem custom class call selector on own class, instead of using custom target, defined method add custom target in case viewcontroller , call there selector
updated
vmbuttoncirclefun.h
// // vmbuttoncirclefun.h // vmbuttoncirclefun // // created vu mai on 6/2/15. // copyright (c) 2015 vumai. rights reserved. // #import <uikit/uikit.h> typedef ns_enum(nsinteger, vmmakelocation) { vmmakelocationtop = 1, vmmakelocationbottom }; @interface vmbuttoncirclefun : uiview @property(nonatomic) uicolor *strokecolor; - (void)addcirclelayerwithtype:(nsinteger)type; - (void)setstrokeend:(cgfloat)strokeend animated:(bool)animated; -(void)buildbutton; -(void)seticonbutton:(uiimage *)icon withtype:(nsinteger)type withcolor:(uicolor*)color; -(void)setlinewidthvalue:(float)linewidthtemp; -(void)addaction:(sel)selector; -(void)addtarget:(id)target; @end
updated
vmbuttoncirclefun.m
// // vmbuttoncirclefun.m // vmbuttoncirclefun // // created vu mai on 6/2/15. // copyright (c) 2015 vumai. rights reserved. // #import "vmbuttoncirclefun.h" @interface vmbuttoncirclefun() @property(nonatomic) cashapelayer *circlelayer; @property (nonatomic) cashapelayer *linelayertoptobottom; @property (nonatomic) cashapelayer *linelayerbottomtohide; @property (nonatomic, strong) uiimageview *imgicon; @property (nonatomic) float linewidth; @property (nonatomic, weak) id target; @end @implementation vmbuttoncirclefun - (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { self.linewidth = 2.0f; } return self; } #pragma mark - instance methods - (void)setstrokeend:(cgfloat)strokeend animated:(bool)animated { self.circlelayer.strokeend = strokeend; } #pragma mark - property setters - (void)setstrokecolor:(uicolor *)strokecolor { self.circlelayer.strokecolor = strokecolor.cgcolor; self.linelayerbottomtohide.strokecolor = strokecolor.cgcolor; self.linelayertoptobottom.strokecolor = strokecolor.cgcolor; _strokecolor = strokecolor; } -(void)setlinewidthvalue:(float)linewidthtemp { self.linewidth = linewidthtemp; } #pragma mark - private instance methods - (void)addcirclelayerwithtype:(nsinteger)type { cgfloat radius; cgfloat linewidth = self.linewidth; cgrect screenrect = [[uiscreen mainscreen] bounds]; // cgfloat screenwidth = screenrect.size.width; cgfloat screenheight = screenrect.size.height; cgrect rect; uibezierpath *path1 = [uibezierpath bezierpath]; uibezierpath *path2 = [uibezierpath bezierpath]; switch (type) { case vmmakelocationtop: radius = cgrectgetwidth(self.bounds)/2 - linewidth/2; rect = cgrectmake(linewidth/2, linewidth/2, radius * 2, radius * 2); [path1 movetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, -screenheight)]; [path1 addlinetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, 1)]; [path2 movetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, 1)]; [path2 addlinetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, -screenheight)]; break; case vmmakelocationbottom: radius = cgrectgetwidth(self.bounds)/2 - linewidth/2; rect = cgrectmake(cgrectgetwidth(self.bounds)-linewidth/2,cgrectgetheight(self.bounds)-linewidth/2, radius * -2, radius * -2); [path1 movetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, +screenheight)]; [path1 addlinetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, cgrectgetheight(self.bounds)-1)]; [path2 movetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, cgrectgetheight(self.bounds)-1)]; [path2 addlinetopoint:cgpointmake(cgrectgetwidth(self.bounds)/2, +screenheight)]; break; default: break; } self.circlelayer = [cashapelayer layer]; self.circlelayer.path = [uibezierpath bezierpathwithroundedrect:rect cornerradius:radius].cgpath; self.circlelayer.strokecolor = self.tintcolor.cgcolor; self.circlelayer.fillcolor = nil; self.circlelayer.linewidth = linewidth; self.circlelayer.linecap = kcalinecapround; self.circlelayer.linejoin = kcalinejoinround; [self.circlelayer setstrokeend:0.0f]; [self.layer addsublayer:self.circlelayer]; self.linelayertoptobottom = [cashapelayer layer]; self.linelayertoptobottom.path = path1.cgpath; self.linelayertoptobottom.linewidth = self.linewidth; [self.linelayertoptobottom setstrokeend:1.0f]; self.linelayertoptobottom.strokecolor = [uicolor darkgraycolor].cgcolor; [self.layer addsublayer:self.linelayertoptobottom];\ self.linelayerbottomtohide = [cashapelayer layer]; self.linelayerbottomtohide.path = path2.cgpath; self.linelayerbottomtohide.linewidth = self.linewidth; [self.linelayerbottomtohide setstrokeend:1.0f]; self.linelayerbottomtohide.strokecolor = [uicolor darkgraycolor].cgcolor; [self.linelayerbottomtohide setopacity:0]; [self.layer addsublayer:self.linelayerbottomtohide]; [uiview setanimationdelegate:self]; } -(void)seticonbutton:(uiimage *)icon withtype:(nsinteger)type withcolor:(uicolor*)color { self.imgicon = [[uiimageview alloc] initwithimage:icon]; [self.imgicon setframe:cgrectmake(0, 0, cgrectgetwidth(self.bounds)-cgrectgetwidth(self.bounds)/2, cgrectgetheight(self.bounds)-cgrectgetheight(self.bounds)/2)]; self.imgicon.image = [icon imagewithrenderingmode:uiimagerenderingmodealwaystemplate]; [self.imgicon setcontentmode:uiviewcontentmodescaleaspectfit]; [self.imgicon settintcolor:color]; switch (type) { case vmmakelocationtop: [self.imgicon setcenter:cgpointmake(cgrectgetwidth(self.bounds)/2, cgrectgetheight(self.bounds)/2 -5 )]; break; case vmmakelocationbottom: [self.imgicon setcenter:cgpointmake(cgrectgetwidth(self.bounds)/2, cgrectgetheight(self.bounds)/2 +5 )]; break; default: break; } [self.imgicon setalpha:0]; [self addsubview:self.imgicon]; } -(void)addaction:(sel)selector { uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self.target action:selector]; [self addgesturerecognizer:tap]; } -(void)addtarget:(id)target { self.target = target; } -(void)buildbutton { cabasicanimation * swipeline = [cabasicanimation animationwithkeypath:@"strokeend"]; [swipeline setvalue:@"toptobottom" forkey:@"id"]; swipeline.delegate = self; swipeline.duration=1; swipeline.fromvalue=[nsnumber numberwithdouble:0.0f]; swipeline.tovalue= [nsnumber numberwithdouble:1.0f]; swipeline.fillmode = kcafillmodeforwards; swipeline.timingfunction= [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; swipeline.removedoncompletion=no; self.linelayertoptobottom.strokeend = 1.0f; [self.linelayertoptobottom addanimation:swipeline forkey:@"toptobottom"]; } -(void)animationlinebottomtohide { cabasicanimation * swipeline1 = [cabasicanimation animationwithkeypath:@"strokeend"]; [swipeline1 setvalue:@"bottomtohide" forkey:@"id"]; swipeline1.duration=1; swipeline1.fromvalue=[nsnumber numberwithdouble:1.0f]; swipeline1.tovalue= [nsnumber numberwithdouble:0.0f]; swipeline1.fillmode = kcafillmodeforwards; swipeline1.timingfunction= [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout]; swipeline1.removedoncompletion=yes; self.linelayerbottomtohide.strokeend = 0.0f; [self.linelayerbottomtohide addanimation:swipeline1 forkey:@"bottomtohide"]; } -(void)animationlinecircle { cabasicanimation * swipe = [cabasicanimation animationwithkeypath:@"strokeend"]; [swipe setvalue:@"drawcircle" forkey:@"id"]; swipe.duration=1; swipe.fromvalue=[nsnumber numberwithdouble:0.0f]; swipe.tovalue= [nsnumber numberwithdouble:1.0f]; swipe.fillmode = kcafillmodeforwards; swipe.timingfunction= [camediatimingfunction functionwithname:kcamediatimingfunctioneaseout]; swipe.removedoncompletion=no; self.circlelayer.strokeend = 1; [self.circlelayer addanimation:swipe forkey:@"drawcircle"]; [uiview animatewithduration:0.5 delay:0.5 options:uiviewanimationoptioncurveeasein animations:^{ [self.imgicon setalpha:1]; [self.imgicon setcenter:cgpointmake(cgrectgetwidth(self.bounds)/2, cgrectgetheight(self.bounds)/2)]; } completion:^(bool finished) { }]; } -(void)animationdidstop:(caanimation *)anim finished:(bool)flag { if([[anim valueforkey:@"id"] isequal:@"toptobottom"]) { [self.linelayerbottomtohide setopacity:1]; [self.linelayertoptobottom setopacity:0]; [self animationlinebottomtohide]; [self animationlinecircle]; } } @end
example viewcontroller, updated
import uikit class viewcontroller: uiviewcontroller { override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. var circleview1 = vmbuttoncirclefun(frame: cgrect(x: cgfloat(0), y: cgfloat(0), width: cgfloat(60), height: cgfloat(60))) circleview1.addcirclelayer(withtype: vmmakelocation.top.rawvalue) circleview1.strokecolor = uicolor(red: 243/255, green: 106/255, blue: 106/255, alpha: 1.0) circleview1.center = cgpoint(x: cgfloat(self.view.bounds.width / 2 - 100), y: cgfloat(self.view.bounds.height / 2)) //circleview1.seticonbutton(uiimage(named: "layer 14.png")!, withtype: vmmakelocation.top.rawvalue, with: uicolor(red: 127/255, green: 140/255, blue: 141/255, alpha: 1.0)) circleview1.setlinewidthvalue(1.0) self.view.addsubview(circleview1) circleview1.buildbutton() circleview1.addtarget(self) circleview1.addaction(#selector(self.logout)) } func logout() { debugprint("logout") } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
i hope helps you, works charm
Comments
Post a Comment