#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[[RootViewController alloc] init] autorelease];
self.window.rootViewController = rootVC;
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
#import "RootViewController.h"
#import "TapView.h"//导入tapView 头文件
#import "WeiTuoView.h" // 导入weituoView 头文件
@interface RootViewController ()<WeiTuoViewDelegate>
@end
@implementation RootViewController
- (void)weiTuoViewChangePositionDidTouchBegin:(WeiTuoView *)weiTuoView
{
weiTuoView.center = [self randomPoint:weiTuoView];
}
- (CGPoint)randomPoint:(UIView *)view{
CGFloat width = view.frame.size.width;
CGFloat height = view.frame.size.height;
CGFloat minX = width / 2;
CGFloat minY = height / 2;
CGFloat maxX = self.view.frame.size.width - minX;
CGFloat maxY = self.view.frame.size.height - minY;
return CGPointMake(arc4random() % (int)(maxX - minX + 1) + minX, arc4random() % (int)(maxY - minY + 1) + minY);
}
- (void)viewDidLoad {
[super viewDidLoad];
WeiTuoView *weituoView = [[WeiTuoView alloc] initWithFrame:(CGRectMake(50, 50, 150, 150))];
weituoView.backgroundColor = [UIColor magentaColor];
weituoView.delegate = self;
[self.view addSubview:weituoView];
[weituoView release];
TapView *colorView = [[TapView alloc] initWithFrame:(CGRectMake(50, 50, 200, 200))];
colorView.backgroundColor = [UIColor yellowColor];
[colorView addTarget:self action:@selector(changeColor:)];
[colorView release];
TapView *sizeView = [[TapView alloc] initWithFrame:(CGRectMake(50, 300, 200, 200))];
sizeView.backgroundColor = [UIColor redColor];
[sizeView addTarget:self action:@selector(changeSize:)];
[sizeView release];
}
#pragma mark -- 改变颜色
- (void)changeColor:(TapView *)tapView{
tapView.backgroundColor = [self randomColor];
}
- (UIColor *)randomColor{
return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}
#pragma mark -- 改变大小
- (void)changeSize:(TapView *)tapView{
CGRect rec1 = tapView.frame;
rec1.size = [self randomSize];
tapView.frame = rec1;
}
- (CGSize)randomSize{
return CGSizeMake(arc4random() % (300 - 50 + 1) + 50, arc4random() % (300 - 50 + 1) + 50);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import <UIKit/UIKit.h>
@class WeiTuoView;
@protocol WeiTuoViewDelegate <NSObject>
- (void)weiTuoViewChangePositionDidTouchBegin:(WeiTuoView *)weiTuoView;
@end
@interface WeiTuoView : UIView<UITextFieldDelegate>
@property (nonatomic, assign) id<WeiTuoViewDelegate> delegate;
@end
#import "WeiTuoView.h"
@implementation WeiTuoView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (_delegate != nil && [_delegate respondsToSelector:@selector(weiTuoViewChangePositionDidTouchBegin:)]) {
[_delegate weiTuoViewChangePositionDidTouchBegin:self];
}
}
@end
#import <UIKit/UIKit.h>
@interface TapView : UIView
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;
- (void)addTarget:(id)target action:(SEL)action;
@end
#import "TapView.h"
@implementation TapView
- (void)addTarget:(id)target action:(SEL)action{
self.target = target;
self.action = action;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (_target != nil && [_target respondsToSelector:_action]) {
[_target performSelector:_action withObject:self];
}
}
@end