代理 Delegeta

Delegate 

􏱋􏰨􏰩􏰒􏱡􏱠􏱢 自定义视图CustomView ,创建一个实例对象,开始点击时视图缩短,移动过程中随机修改颜色,结束点击时视图恢复宽度

#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];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    RootViewController *rootVC = [[RootViewController alloc]init];

    self.window.rootViewController = rootVC;

    [rootVC release];

 

    [self.window makeKeyAndVisible];

    return YES;

}

 

 

 

 

 

 

 

#import "RootViewController.h"

#import "TouchView.h"

@interface RootViewController () <TouchViewDelegeta>

 

@end

 

@implementation RootViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    TouchView *view1 = [[TouchView alloc]initWithFrame:CGRectMake(30, 100, 300, 300)];

    view1.backgroundColor = [UIColor magentaColor];

    [self.view addSubview:view1];

    view1.delegeta = self;

    [view1 release];

    

}

 

- (void)touchViewBeginTouched:(TouchView *)touchView

{

    touchView.bounds = CGRectMake(0, 0, 150, 150);

}

 

- (void)touchViewMovedTouched:(TouchView *)touchView

{

    touchView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];

}

 

- (void)touchViewEndTouch:(TouchView *)touchView

{

    touchView.bounds = CGRectMake(0, 0, 300, 300);

}

 

 

 

 

#import <UIKit/UIKit.h>

 

@class TouchView;

 

@protocol TouchViewDelegeta <NSObject>

 

@optional

 

- (void)touchViewBeginTouched:(TouchView *)touchView;

- (void)touchViewMovedTouched:(TouchView *)touchView;

- (void)touchViewEndTouch:(TouchView *)touchView;

 

@end

 

 

@interface TouchView : UIView

 

@property(nonatomic,assign)id<TouchViewDelegeta> delegeta;

 

@end

 

 

 

 

 

#import "TouchView.h"

 

@implementation TouchView

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    if (_delegeta && [_delegeta respondsToSelector:@selector(touchViewBeginTouched:)]) {

        [_delegeta touchViewBeginTouched:self];

    }

}

 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    if (_delegeta && [_delegeta respondsToSelector:@selector(touchViewMovedTouched:)]) {

        [_delegeta touchViewMovedTouched:self];

    }

}

 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

    if (_delegeta && [_delegeta respondsToSelector:@selector(touchViewEndTouch:)]) {

        [_delegeta touchViewEndTouch:self];

    }

}

 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

{

    

}

 

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

 

@end

posted @ 2015-08-25 23:43  Hooy星  阅读(193)  评论(0编辑  收藏  举报