废话不多说,直接上代码,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
|