カスタムbutton


独自のコードライブラリを構築し、独自の工場を構築する必要があります.
アップル社は私たちに強力な利器を提供してくれた.
しかし、基本的な機能を簡単に実現することで満足するべきではありません.
牛の成長の道は、自分でゆっくりと研究している.
我々は牛に成長する目標を持たなければならない.
今日は初心者にとってカスタムbuttonを書くことが大切です
アップル社が提供するクラスライブラリを理解しなければなりません
私たちが書いたプログラムはできるだけ原生に近づくべきだ.
このようにして、私たちのプログラムは効率などの便利さを理解しやすくなります.
コードをつけました:コードは长くありませんて、しかし1种の思想はよく理解します
//
//  MyButton.h
//   Button

#import <UIKit/UIKit.h>

@interface MyButton : UIControl

/**
 *   
 *
 *  @param title    <#title description#>
 *  @param frame    <#frame description#>
 *  @param target   <#target description#>
 *  @param selector <#selector description#>
 *
 *  @return <#return value description#>
 */

+(MyButton *)createButtonWithTitle:(NSString *)title andFrame:(CGRect)frame andTarget:(id)target andSelector:(SEL)selector;

@end
//
//  MyButton.m
//   Button
//

#import "MyButton.h"

@implementation MyButton



+(MyButton *)createButtonWithTitle:(NSString *)title andFrame:(CGRect)frame andTarget:(id)target andSelector:(SEL)selector
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //UIButton *btn = [[UIButton alloc]init];
    btn.frame = frame;
    
    btn.layer.cornerRadius =10.0f;
    
    btn.layer.masksToBounds = YES;
    
    btn.layer.borderWidth = 2.0;
    
    btn.backgroundColor = [UIColor colorWithRed:0.18f green:0.64f blue:0.87f alpha:1.00f];
    [btn setTitle:title forState:UIControlStateNormal];
    
    [btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}
@end
//
//  ViewController.h
//   Button


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end
//
//  ViewController.m
//   Button
//


#import "ViewController.h"
#import "MyButton.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    MyButton *btn = [MyButton createButtonWithTitle:@"Happy" andFrame:CGRectMake(10, 30, 100, 50) andTarget:self andSelector:@selector(btnClick)];
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    [self.view addSubview:btn];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)btnClick
{
    NSLog(@" !");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end