<AppDelegate.m>
</pre><pre name="code" class="objc">#import "AppDelegate.h" #import "RootViewController.h" @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; RootViewController *rootVC = [[RootViewController alloc] init]; self.window.rootViewController = rootVC; [rootVC release]; return YES; } - (void)applicationWillResignActive:(UIApplication *)application { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - (void)applicationDidEnterBackground:(UIApplication *)application { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - (void)applicationWillEnterForeground:(UIApplication *)application { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - (void)applicationDidBecomeActive:(UIApplication *)application { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } @end
<RootViewController.h>
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
<RootViewController.m>
</pre><pre name="code" class="objc">#import "RootViewController.h" #import "TouchView.h" @interface RootViewController ()<TouchViewDelegate> @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //touchView1 TouchView *touchView1 = [[TouchView alloc] initWithFrame:CGRectMake(50, 100, 220, 100)]; touchView1.backgroundColor = [UIColor blueColor]; //设置代理 touchView1.delegate = self; [self.view addSubview:touchView1]; [touchView1 release]; //touchView2 TouchView *touchView2 = [[TouchView alloc] initWithFrame:CGRectMake(50, 250, 220, 100)]; touchView2.backgroundColor = [UIColor purpleColor]; touchView2.delegate = self; [self.view addSubview:touchView2]; [touchView2 release]; //touchView3 TouchView *touchView3 = [[TouchView alloc]initWithFrame:CGRectMake(50, 400, 220, 100)]; touchView3.backgroundColor = [UIColor orangeColor]; touchView3.delegate = self; [self.view addSubview:touchView3]; [touchView3 release]; //设置 tag 值 touchView1.tag = 100; touchView2.tag = 101; touchView3.tag = 102; } -(void)touchView:(TouchView *)touchView whenTouchBegan:(NSString *)what { NSLog(@"%@",what); //根据 tag 值判断执行那个过程 if (touchView.tag == 100) { //颜色改变 touchView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random()%256 / 255.0 blue:arc4random()%256 / 255.0 alpha:1]; } else if(touchView.tag == 101) { //位置 touchView.center = CGPointMake(arc4random()%200, arc4random()%200); }else if(touchView.tag == 102) { touchView.bounds = CGRectMake(0, 0, arc4random()%200 + 10, arc4random()%150 + 10); //以中心点为标准来改变大小 } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end
<TouchView.h>
</pre><pre name="code" class="objc">#import <UIKit/UIKit.h> @class TouchView; //创建协议 @protocol TouchViewDelegate <NSObject> //当视图 touchbegan 时,执行的方法 -(void)touchView:(TouchView *)touchView whenTouchBegan:(NSString *)what; @end @interface TouchView : UIView @property (nonatomic,assign)id<TouchViewDelegate> delegate;//assign 必须要写,否则会报警告 @end
<TouchView.m>
</pre><pre name="code" class="objc">#import "TouchView.h" @implementation TouchView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //此时让代理执行协议中的方法 [_delegate touchView:self whenTouchBegan:@"what?"]; } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end