分别用label和imageView来实现button

用label和imageView实现button的功能 其中点击方法使用手势来写的

首先创建两个类 一个是用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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,755评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,695评论 25 709
  • 都说四月不减肥,五月徒悲伤。而我就是今天不减肥,明天徒悲伤。 我就是那个12个月都在悲伤的胖子 我胖我骄傲 1. ...
    疯起疯落阅读 3,332评论 3 1
  • 宝贝儿,今天是你出生的第七十天,还有一个月就百天了,有时候觉得时间过的真慢呀,我最爱的小麦穗什么时候能长大呢;而有...
    3237f5d4b965阅读 1,166评论 0 0
  • 1、网络沟通2个 2、团队学习与交流 个人体会 由于这几天河北来的资深老师在店里,她来一次也是相当不容易,...
    葆婴USANA廖瑜阅读 1,517评论 0 0

友情链接更多精彩内容