1 implementation of the Round button (the Click area is also circular)
The system UIButton comes in a rectangular way, and by setting the layer layer you can crop the picture to a circle, but the click area is still a rectangle. To solve this problem, the method of this article is to create a UIButton subclass, overriding the UIButton-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *) method of the event. The code is as follows:
1-(BOOL) Pointinside: (cgpoint) point withevent: (Uievent *)Event2 {3BOOL flag = [Super Pointinside:point withevent:Event];4 if(flag) {5Uibezierpath *path =[Uibezierpath bezierPathWithOvalInRect:self.bounds];6 if([path Containspoint:point]) {7 returnYES;8 }9 returnNO;Ten } One returnNO; A}
The idea of this method is to intercept the touch-click event Response function, use Uibezierpath to set the click Area, and not handle events outside the click Area.
2 Pictures again on the implementation of the title under button
iOS comes with the UIButton system style: The picture is on the left, the title is on the right, sometimes the demand is the picture again, the title again. If you redefine a view, the corresponding event should be implemented on its own, too complicated. The method of this article is to create a UIButton subclass that overrides the two system methods of UIButton. Specifically:-(CGRect) Imagerectforcontentrect: (CGRect) Contentrect and-(CGRect) Titlerectforcontentrect: (CGRect) ContentRect. By keyword, you can see that the former overrides the position of the graphic in the button, which overrides the position of the caption in the button. The code is as follows:
1-(CGRect) Imagerectforcontentrect: (cgrect) Contentrect2 {3CGFloat Imagey =2.5;4CGFloat Imagew = -;5CGFloat Imageh = -;6CGFloat ImageX = (Contentrect.size.width-imagew)/2.0;7 8CGRect Imagerect =CGRectMake (ImageX, Imagey, Imagew, Imageh);9 returnImagerect; One } - --(CGRect) Titlerectforcontentrect: (cgrect) Contentrect the { -CGFloat Titlex =0; -CGFloat Titley = -; -CGFloat Titlew =ContentRect.size.width; +CGFloat Titleh = contentRect.size.height-Titley; - +CGRect Titlerect =CGRectMake (Titlex, Titley, Titlew, Titleh); at returnTitlerect; - } - -- (void) Setcomparebtnattribute - { - [self settitlecolor:timercolor forstate:uicontrolstatenormal]; in [self Settitlecolor:[uicolor lightgraycolor] forstate:uicontrolstatehighlighted]; - toSelf.titleLabel.font = [Uifont systemfontofsize: One]; +Self.titleLabel.textAlignment =Nstextalignmentcenter; - the}
The view layout takes a fixed approach, and the third function is to set the same properties for multiple buttons of the same type.
3 No, follow up. Post your source code on GitHub, and welcome to the exchange. Original, without permission, no reprint.
iOS round buttons and pictures on the top of the title in the Next button implementation