假如有一对按钮【重置】【提交】,要让他们的默认UI和点击的UI表现刚好相反

【提交】按钮,默认橙色,点击边框是橙色,字体是橙色,背景变白色

【重置】按钮,默认白色橙色,边框是橙色,点击字体是白色,背景变橙色

@implementation SubmmitButton

- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
self = [super initWithFrame:frame];
if (self) {
UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
// UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
self = [SubmmitButton new];
[self setTitle:title forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setBackgroundImage:image forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont systemFontOfSize:16]];
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:6];
}
return self;
} - (void)setSelected:(BOOL)selected {
[super setSelected:selected];
} - (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
if (highlighted) {
[self setBackgroundImage:selectedImage forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor orangeColor].CGColor;
}else {
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ @end
@implementation RestButton
- (instancetype)initWithFrame:(CGRect)frame withTitle:(NSString *)title {
self = [super initWithFrame:frame];
if (self) {
// UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
self = [RestButton new];
[self setTitle:title forState:UIControlStateNormal];
[self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[self setBackgroundImage:selectedImage forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont systemFontOfSize:16]];
[self.layer setMasksToBounds:YES];
[self.layer setCornerRadius:6];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor orangeColor].CGColor;
}
return self;
} - (void)setSelected:(BOOL)selected {
[super setSelected:selected];
} - (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
UIImage *image = [UIImage createImageWithColor:[UIColor orangeColor]];
UIImage *selectedImage = [UIImage createImageWithColor:[UIColor whiteColor]];
if (highlighted) {
[self setBackgroundImage:image forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}else {
[self setBackgroundImage:selectedImage forState:UIControlStateNormal];
[self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor orangeColor].CGColor;
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ @end

UIImage的一个分类方法

/**
根据颜色生图片 @param color 生成图片的颜色
@return 返回纯色图片
*/
+ (UIImage*)createImageWithColor:(UIColor*)color {
CGRect rect = CGRectMake(0.0f,0.0f,10.0f,10.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
return theImage;
}

iOS 自定义一对UI表现相反的按钮的更多相关文章

  1. iOS 自定义图片和文字垂直显示按钮<上面是图片,文字显示下面>

    #import <UIKit/UIKit.h> @interface VerticalButton : UIButton @end #import "VerticalButton ...

  2. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  3. iOS点击获取短信验证码按钮

    概述 iOS点击获取短信验证码按钮, 由于 Demo整体测试运行效果 , 整个修改密码界面都已展现, 并附送正则表达式及修改密码逻辑. 详细 代码下载:http://www.demodashi.com ...

  4. React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发

    React Native实战系列教程之自定义原生UI组件和VideoView视频播放器开发   2016/09/23 |  React Native技术文章 |  Sky丶清|  4 条评论 |  1 ...

  5. iOS自定义转场动画实战讲解

    iOS自定义转场动画实战讲解   转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...

  6. amazeui学习笔记--js插件(UI增强2)--按钮交互Button

    amazeui学习笔记--js插件(UI增强2)--按钮交互Button 一.总结 1.按钮loading状态: <button type="button" class=&q ...

  7. iOS系列 基础篇 06 标签和按钮 (Label & Button)

    iOS系列 基础篇 06 标签和按钮 (Label & Button) 目录: 标签控件 按钮控件 小结 标签和按钮是两个常用的控件,下面咱们逐一学习. 1. 标签控件 使用Single Vi ...

  8. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  9. 免费素材下载:iOS 8 矢量 UI 素材套件

    小伙伴们,苹果终于在今天凌晨推送了 iOS 8 的正式版.虽然该系统并未与 iPhone6 发布会同时亮相,但对于已经提前体验尝鲜过测试版的同学来说并不陌生.iOS 8 几乎每个图标都进行了重新设计, ...

随机推荐

  1. 30分钟Maven入门到精通

    Maven是近年来最流行的项目构建与管理工具.不仅简化了我们开发过程中对jar包依赖的导入,还对项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等所有构建过程进行了抽象和统一,方便 ...

  2. Deadlock_synchromized-Java_se

    class Test implements Runnable{ private boolean flag; Test(boolean flag) { this.flag = flag; } publi ...

  3. JSON.parse()与JSON.stringify()和eval()使用方法详解

    在和后端对接口的时候,遇到了一个问题 就是series里面数据变量进行拼接的时候,data数据里面全部是数值int类型的 但是因为某些需求需要让他进行某个数据之前的数据都为空 我试过用空字符串和und ...

  4. Laravel5如何向闭合函数内传递参数 where function 传参

    如上,怎么将$title 像$query一样,在函数内部使用? $result = UserMenus::with(['menu'=>function($query){ $query->w ...

  5. Codeforces 985 最短水桶分配 沙堆构造 贪心单调对列

    A B /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a, ...

  6. Vim搜索关键字

    有以下两种方法 Method 1:/content 默认从上往下查找 只读模式下输入 /content 后回车 按 n 向下查找 按N 向上查找 Method 2:?content 默认从下往上查找 ...

  7. vue2.0 笔记(杂记)

    一.vue class.style表达式的类型: 字符串.对象和数组1.字符串 <div class="static" v-bind:class="class-a& ...

  8. C++打开属性对话框并保持其处于打开状态

    #include <Windows.h> #include <shlobj_core.h> #pragma comment(lib,"Shell32.lib" ...

  9. CodeForces-687A(DFS,染色)

    链接: https://vjudge.net/problem/CodeForces-687A 题意: Recently, Pari and Arya did some research about N ...

  10. 用Chrome 浏览器调试移动端网页 chrome://inspect/#devices

    谷歌输入(chrome://inspect/#devices) 我使用的是小米(红米NOTE2 ),电脑是win 10 系统,以下几步就可以轻松使用浏览器内置的功能调试移动端网页了: 1. 手机开启调 ...