黑马程序员技术交流社区
标题:
关于纯代码搭建UIButtion的问题
[打印本页]
作者:
飞扬的青春haha
时间:
2016-6-11 23:00
标题:
关于纯代码搭建UIButtion的问题
今天学到存代码搭建ui,然后一一共创建了5个按钮,其中想通过其中的4个按钮来控制第五个按钮的左右上下移动,请问怎么实现
作者:
1871037345
时间:
2016-6-13 13:54
移动的过程其实是一个简单的动画效果,包括缩放,旋转都是,还是自己动手试试吧
作者:
SSIrreplaceable
时间:
2016-6-17 16:42
我自学了然后写了博客你参考一下:http://blog.csdn.net/ssirreplaceable/article/details/51405094
作者:
FFFF001
时间:
2016-6-23 09:50
废话不多说,直接上代码,Ipone 6S 4.7寸:
//
// ViewController.m
// FF-6-23-01
//
// Created by ya on 16/6/23.
// Copyright © 2016年 FF0002. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIButton *moveBtn;
@property (nonatomic, assign) CGPoint moveBtnCenter;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_moveBtn = [[UIButton alloc] initWithFrame:CGRectMake(122, 141, 131, 93)];
[_moveBtn setBackgroundColor:[UIColor redColor]];
[_moveBtn setTitle:@"移动我吧" forState:UIControlStateNormal];
[self.view addSubview:_moveBtn];
_moveBtnCenter = _moveBtn.center;
NSArray *strArray = [[NSArray alloc] initWithObjects:@"上", @"下", @"左", @"右", nil];
for (int i = 0; i < 4; i ++)
{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i * (50 + 35) + 35, 412, 50, 30)];
[btn setBackgroundColor:[UIColor greenColor]];
[btn setTitle:[strArray objectAtIndex:i] forState:UIControlStateNormal];
[btn setTag:i];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake(146, 514, 83, 52)];
[resetBtn setTitle:@"复位" forState:UIControlStateNormal];
[resetBtn setBackgroundColor:[UIColor grayColor]];
[resetBtn setTag:4];
[resetBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:resetBtn];
}
#define Upward 0
#define Downward 1
#define Left 2
#define Right 3
#define Reset 4
- (void)btnClick:(UIButton *)btn
{
switch (btn.tag) {
case Upward:
{
[UIButton animateWithDuration:1 animations:^{
_moveBtn.center = CGPointMake(_moveBtn.center.x, _moveBtn.center.y - 50);
}];
}
break;
case Downward:
{
[UIButton animateWithDuration:1 animations:^{
_moveBtn.center = CGPointMake(_moveBtn.center.x, _moveBtn.center.y + 50);
}];
}
break;
case Left:
{
[UIButton animateWithDuration:1 animations:^{
_moveBtn.center = CGPointMake(_moveBtn.center.x - 50, _moveBtn.center.y);
}];
}
break;
case Right:
{
[UIButton animateWithDuration:1 animations:^{
_moveBtn.center = CGPointMake(_moveBtn.center.x + 50, _moveBtn.center.y);
}];
}
break;
case Reset:
{
[UIButton animateWithDuration:1 animations:^{
_moveBtn.center = _moveBtnCenter;
}];
}
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2