#import "WViewController.h"
#define WLEFT 2
#define WBTN 1
#define WRIGHT 3
#define WUP 4
#define WDOWN 5
#define PLEFT (CGPoint){thepoint.x - 30 , thepoint.y}
#define PRIGHT (CGPoint){thepoint.x + 30 , thepoint.y}
#define PUP (CGPoint){thepoint.x , thepoint.y - 30}
#define PDOWN (CGPoint){thepoint.x , thepoint.y +30}
#define WCCW 10
#define WCW 20
#define WADD 30
#define WREMOTE 40
#define PRCCW (CGPoint){thepoint.x +200 , thepoint.y -30}
#define PRCW (CGPoint){thepoint.x + 150 , thepoint.y -30}
#define PADD (CGPoint){thepoint.x +200 , thepoint.y + 20}
#define PREMOTE (CGPoint){thepoint.x +150 , thepoint.y + 20}
@interface WViewController ()
{
}
@end
@implementation WViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self createGreenBtn];
CGPoint thepoint = CGPointMake(70, 280);
// 根据thepoint创建整个游戏按钮界面。
[self createDirection:@"sub_black_prev" highl:@"sub_blue_prev" frame:PLEFT tag:WLEFT];
[self createDirection:@"sub_black_next" highl:@"sub_blue_next" frame:PRIGHT tag:WRIGHT];
[self createDirection:@"sub_black_up" highl:@"sub_blue_up" frame:PUP tag:WUP];
[self createDirection:@"sub_black_down" highl:@"sub_blue_down" frame:PDOWN tag:WDOWN];
[self createDirection:@"sub_black_rotate_ccw" highl:@"sub_blue_rotate_ccw" frame:PRCCW tag:WCCW];
[self createDirection:@"sub_black_rotate_cw" highl:@"sub_blue_rotate_cw" frame:PRCW tag:WCW];
[self createDirection:@"sub_black_add" highl:@"sub_blue_add" frame:PADD tag:WADD];
[self createDirection:@"sub_black_remove" highl:@"sub_blue_remove" frame:PREMOTE tag:WREMOTE];
}
- (void)createGreenBtn
{
UIButton* btn = [[UIButton alloc ] initWithFrame:CGRectMake(100, 100, 100, 100)];
[btn setBackgroundImage:[UIImage imageNamed:@"button.jpg"] forState:UIControlStateNormal];
[btn setTag:WBTN];
[self.view addSubview:btn];
}
// 通过自定义构造函数创建button 设定图片, 位置和标签
- (void)createDirection:(NSString*)name1 highl:(NSString*)name2 frame:(CGPoint)thep tag:(NSInteger)tag
{
UIImage* img1 = [UIImage imageNamed:name1];
UIImage* img2 = [UIImage imageNamed:name2];
UIButton* button = [[UIButton alloc] initWithFrame:(CGRect){thep, img1.size}];
[button setTag:tag];
[button setBackgroundImage:img1 forState:UIControlStateNormal];
[button setBackgroundImage:img2 forState:UIControlStateHighlighted];
[self.view addSubview:button];
[button addTarget:self action:@selector(directionClick:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)directionClick:(UIButton*)btn
{
// 根据self.view中的tag值查找对象 重要!!
UIView* button = [self.view viewWithTag:WBTN];
CGPoint point = button.center;
CGFloat delta = 30;
// if (button.frame.origin.x > self.view.frame.size.width) {
// [button setCenter:CGPointMake(- button.frame.size.width/2, button.frame.origin.y)] ;
// }
// if (button.frame.origin.x < - button.frame.size.width) {
// [button setCenter:CGPointMake(self.view.frame.size.width + button.frame.size.width/2, button.frame.origin.y)] ;
// }
// if (button.frame.origin.y < - button.frame.size.height) {
// [button setFrame:(CGRect){CGPointMake(button.frame.origin.x, self.view.frame.size.height), button.frame.size}];
// }
// if (button.frame.origin.y > self.view.frame.size.height) {
// [button setFrame:(CGRect){CGPointMake(button.frame.origin.x, - button.frame.size.height) , button.frame.size}];
// }
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.2f];
switch (btn.tag) {
case WLEFT:
[button setCenter:CGPointMake(point.x - delta, point.y)];
break;
case WRIGHT:
[button setCenter:CGPointMake(point.x + delta, point.y)];
break;
case WUP:
[button setCenter:CGPointMake(point.x , point.y - delta)];
break;
case WDOWN:
[button setCenter:CGPointMake(point.x , point.y + delta)];
break;
case WCCW:
// [button setTransform:CGAffineTransformRotate(button.transform, - )];
// NSLog(@"wccw"); // 逆时针
break;
case WCW:
[button setTransform:CGAffineTransformRotate(button.transform, M_PI_4)];
break;
case WADD:
[button setTransform:CGAffineTransformScale(button.transform, 1.2, 1.2)];
// - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
// - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
break;
case WREMOTE:
[button setTransform:CGAffineTransformScale(button.transform, 5.0/6, 5.0/6)];
break;
// CG_EXTERN CGAffineTransform CGAffineTransformTranslate(CGAffineTransform t,
// CGFloat tx, CGFloat ty) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
//
// /* Scale `t' by `(sx, sy)' and return the result:
// t' = [ sx 0 0 sy 0 0 ] * t */
//
// CG_EXTERN CGAffineTransform CGAffineTransformScale(CGAffineTransform t,
// CGFloat sx, CGFloat sy) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
//
// /* Rotate `t' by `angle' radians and return the result:
// t' = [ cos(angle) sin(angle) -sin(angle) cos(angle) 0 0 ] * t */
//
// CG_EXTERN CGAffineTransform CGAffineTransformRotate(CGAffineTransform t,
// CGFloat angle) CG_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
//
// /* Invert `t' and return the result. If `t' has zero determinant, then `t'
// is returned unchanged. */
}
[UIView commitAnimations];
}
这篇文章写的挺好,推荐一下http://donbe.blog.163.com/blog/static/138048021201061054243442/