iOS开发基础篇-手写控件

时间:2023-03-08 16:39:10

一、手写控件的步骤

  1)使用相应的控件类创建控件对象;

  2)设置该控件的各种属性;

  3)添加空间到视图中;

  4)如果是 UIButton 等控件,还需考虑控件的单击事件等;

二、添加 UIButton 单击事件

   [topbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];

  1) addTarget:forControlEvents: 方法定义在 UIControl 类中,意味着所有继承自 UIControl 类的对象均可添加;

  2)第一个参数为对象本身;

  3)第二个参数为监听控件的事件。

 @interface ViewController ()
@property (nonatomic, weak) IBOutlet UIButton *headImageView;  //被控制用于移动的按钮
@end
 //手工添加上下左右、放大缩小按钮
1 - (void)viewDidLoad {
[super viewDidLoad];
//设置按钮对象为自定义型
UIButton *headbtn = [UIButton buttonWithType:UIButtonTypeCustom];
//设置按钮对象普通状态下的各项属性
headbtn.frame = CGRectMake(, , , );
[headbtn setBackgroundImage:[UIImage imageNamed:@"beauty10"] forState:UIControlStateNormal];
[headbtn setTitle:@"点我!" forState:UIControlStateNormal];
[headbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//设置按钮对象高亮状态下的属性
[headbtn setBackgroundImage:[UIImage imageNamed:@"beauty11"] forState:UIControlStateHighlighted];
[headbtn setTitle:@"还行吧~~" forState:UIControlStateHighlighted];
[headbtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
//将按钮对象添加到视图中显示出来
[self.view addSubview:headbtn];
self.headImageView = headbtn;  //建立IBOutlet连接 //向上移动的按钮
UIButton *topbtn = [UIButton buttonWithType:UIButtonTypeCustom];
topbtn.frame = CGRectMake(, , , );
[topbtn setTitle:@"上移" forState:UIControlStateNormal];
[topbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[topbtn setTag:];
[self.view addSubview:topbtn];
//添加按钮的单击事件
[topbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //向下移动的按钮
UIButton *downbtn = [UIButton buttonWithType:UIButtonTypeCustom];
downbtn.frame = CGRectMake(, , , );
[downbtn setTitle:@"下移" forState:UIControlStateNormal];
[downbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[downbtn setTag:];
[self.view addSubview:downbtn];
//添加按钮的单击事件
[downbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //向左移动的按钮
UIButton *leftbtn = [UIButton buttonWithType:UIButtonTypeCustom];
leftbtn.frame = CGRectMake(, , , );
[leftbtn setTitle:@"左移" forState:UIControlStateNormal];
[leftbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[leftbtn setTag:];
[self.view addSubview:leftbtn];
//添加按钮的单击事件
[leftbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //向右移动的按钮
UIButton *rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
rightbtn.frame = CGRectMake(, , , );
[rightbtn setTitle:@"右移" forState:UIControlStateNormal];
[rightbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[rightbtn setTag:];
[self.view addSubview:rightbtn];
//添加按钮的单击事件
[rightbtn addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside]; //放大的按钮
UIButton *zoombtn = [UIButton buttonWithType:UIButtonTypeCustom];
zoombtn.frame = CGRectMake(, , , );
[zoombtn setTitle:@"放大" forState:UIControlStateNormal];
[zoombtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[zoombtn setTag:];
[self.view addSubview:zoombtn];
//添加按钮的单击事件
[zoombtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside]; //缩小的按钮
UIButton *minusbtn = [UIButton buttonWithType:UIButtonTypeCustom];
minusbtn.frame = CGRectMake(, , , );
[minusbtn setTitle:@"缩小" forState:UIControlStateNormal];
[minusbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[minusbtn setTag:];
[self.view addSubview:minusbtn];
//添加按钮的单击事件
[minusbtn addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside];
}

  先声明上下左右移动的枚举类型,及移动或放大缩小的常量值:

 //ViewController.m
typedef NS_ENUM(NSUInteger, kMovingDir)
{
kMovingDirTop = ,
kMovingDirButtom,
kMovingDirLeft,
kMovingDirRight,
}; CGFloat const kMovingDelta = 50.0; //移动系数
CGFloat const kZoomingDelta = 50.0; //放大缩小系数

  实现按钮的事件:

 - (void)move:(id)sender {
UIButton *button = (UIButton *)sender; CGPoint p = self.headImageView.center;
switch (button.tag) {
case kMovingDirTop: p.y -= kMovingDelta; break; //往上移动
case kMovingDirButtom: p.y += kMovingDelta; break; //往下移动
case kMovingDirLeft: p.x -= kMovingDelta; break; //往左移动
case kMovingDirRight: p.x += kMovingDelta; break; //往右移动
} [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.center = p;
[UIView commitAnimations];
} - (void)zoom:(id)sender {
UIButton *button = (UIButton *)sender; CGRect rect = self.headImageView.bounds;
if (button.tag) { //缩小
rect.size.width -= kZoomingDelta;
rect.size.height -= kZoomingDelta;
} else { //放大
rect.size.width += kZoomingDelta;
rect.size.height += kZoomingDelta;
} [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
[UIView commitAnimations];
}

示图如下:

iOS开发基础篇-手写控件

示例代码:http://pan.baidu.com/s/1i3SainN

三、简单的动画

  1)开始动画;

  2)设置动画相关的参数,如时间等;

  3)参与动画的行动

  4)提交动画。

示例代码如下:

     [UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
self.headImageView.bounds = rect;
[UIView commitAnimations];

参考博客:iOS开发UI基础—手写控件,frame,center和bounds属性