分別用label和imageView來實現button

用label和imageView實現button的功能 其中點擊方法使用手勢來寫的

首先創(chuàng)建兩個類 一個是用label實現button的類 另一個是用imageView來實現button的類 這兩個類都是“+”方法 都繼承于UIView 返回值分別是label和imageView

#import <UIKit/UIKit.h>

@interface LabelButton : UIView
+ (UILabel *)createButtonFrame:       (CGRect)frame view:(UIView *)view    buttonName:(NSString *)buttonName color:(UIColor *)color tap:  (UITapGestureRecognizer *)tap;

@end


#import "LabelButton.h"

@implementation LabelButton
+ (UILabel *)createButtonFrame:    (CGRect)frame view:(UIView *)view buttonName:(NSString *)buttonName color:(UIColor *)color tap:(UITapGestureRecognizer *)tap {
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = buttonName;
label.backgroundColor = color;
[view addSubview:label];
label.userInteractionEnabled = YES;
[label addGestureRecognizer:tap];

return label;
}

@end


#import <UIKit/UIKit.h>

@interface ImageViewButton : UIView
+ (UIImageView *)createButtonFrame:(CGRect)frame view:(UIView *)view image:(UIImage *)image tap:(UITapGestureRecognizer *)tap;

@end

#import "ImageViewButton.h"

@implementation ImageViewButton
+ (UIImageView *)createButtonFrame: (CGRect)frame view:(UIView *)view image:(UIImage *)image tap:(UITapGestureRecognizer *)tap {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
UIImage *buttonImage = image;
[imageView setImage:buttonImage];
imageView.userInteractionEnabled = YES;
[view addSubview:imageView];
[imageView addGestureRecognizer:tap];
return imageView;
}
@end


#import "ViewController.h"
#import "LabelButton.h"
#import "ImageViewButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
UILabel *label = [[UILabel alloc] init];
UITapGestureRecognizer *labelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick:)];
label = [LabelButton createButtonFrame:CGRectMake(20, 100, 90, 30) view:self.view buttonName:@"確認" color:[UIColor redColor] tap:labelTap];

UIImageView *imageView = [[UIImageView alloc]init];
UITapGestureRecognizer *imageButtonTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)];
imageView = [ImageViewButton createButtonFrame:CGRectMake(20, 150, 260, 150) view:self.view image:[UIImage imageNamed:@"u=3730821670,510701542&fm=21&gp=0.jpg"] tap:imageButtonTap];

}
- (void)labelClick:(UITapGestureRecognizer *)tap {
NSLog(@"確認就確認唄");
}

- (void)imageClick:(UITapGestureRecognizer *)tap {
NSLog(@"我最可愛 哼");
}

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

@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機僧诚,發(fā)現死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蝗碎,“玉大人振诬,你說我怎么就攤上這事⊙芰猓” “怎么了赶么?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長脊串。 經常有香客問我辫呻,道長,這世上最難降的妖魔是什么琼锋? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任放闺,我火速辦了婚禮,結果婚禮上缕坎,老公的妹妹穿的比我還像新娘怖侦。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布匾寝。 她就那樣靜靜地躺著搬葬,像睡著了一般。 火紅的嫁衣襯著肌膚如雪艳悔。 梳的紋絲不亂的頭發(fā)上急凰,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音猜年,去河邊找鬼抡锈。 笑死,一個胖子當著我的面吹牛乔外,可吹牛的內容都是我干的床三。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼杨幼,長吁一口氣:“原來是場噩夢啊……” “哼勿璃!你這毒婦竟也來了?” 一聲冷哼從身側響起推汽,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎歧沪,沒想到半個月后歹撒,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡诊胞,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年暖夭,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片撵孤。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡迈着,死狀恐怖,靈堂內的尸體忽然破棺而出邪码,到底是詐尸還是另有隱情裕菠,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布闭专,位于F島的核電站奴潘,受9級特大地震影響,放射性物質發(fā)生泄漏影钉。R本人自食惡果不足惜画髓,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望平委。 院中可真熱鬧奈虾,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至浪册,卻和暖如春扫腺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背村象。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工笆环, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人厚者。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓躁劣,卻偏偏與公主長得像,于是被迫代替她去往敵國和親库菲。 傳聞我的和親對象是個殘疾皇子账忘,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容

  • 發(fā)現 關注 消息 iOS 第三方庫、插件熙宇、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,102評論 4 62
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,107評論 25 707
  • 都說四月不減肥鳖擒,五月徒悲傷。而我就是今天不減肥烫止,明天徒悲傷蒋荚。 我就是那個12個月都在悲傷的胖子 我胖我驕傲 1. ...
    瘋起瘋落閱讀 445評論 3 1
  • 寶貝兒,今天是你出生的第七十天馆蠕,還有一個月就百天了期升,有時候覺得時間過的真慢呀,我最愛的小麥穗什么時候能長大呢互躬;而有...
    3237f5d4b965閱讀 107評論 0 0
  • 1?網絡溝通2個 2?團隊學習與交流 個人體會 由于這幾天河北來的資深老師在店里播赁,她來一次也是相當不容易,...
    葆嬰USANA廖瑜閱讀 185評論 0 0