iOS-------delegate与UITouch

//

//  CustomView.h

//  Delegate

//

//  Created by feizhu on 14-9-19.

//  Copyright (c) 2014 www.feizhu.com. All rights reserved.

//



#import


@class CustomView;


@protocol CustomViewDelegate <NSObject>


@optional

//customView开始被触摸时

- (void)customViewBeganTouch:(CustomView *)customView;



//customView被触摸的过程中

- (void)customViewMovedTouch:(CustomView *)customView;



//customView结束被触摸时

- (void)customViewEndTouch:(CustomView *)customView;



@end


@interface CustomView : UIView


//声明属性

@property (nonatomic,assign) id<CustomViewDelegate>delegate;



@end

视图.m为:

//

//  CustomView.m

//  Delegate

//

//  Created by feizhu on 14-9-19.

//  Copyright (c) 2014 www.feizhu.com. All rights reserved.

//


#import "CustomView.h"

#import "RootViewController.h"

@implementation CustomView


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}


//响应

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

{

    //customVie开始被触摸式,通知代理执行相应的代理方法,紧接着 实现某种操作.

    if ([_delegate respondsToSelector:@selector(customViewBeganTouch:)])

    {

        

        [_delegate customViewBeganTouch:self];

        

        

    }


}

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

{

    if ([_delegate respondsToSelector:@selector(customViewMovedTouch:)])

    {

        [_delegate customViewMovedTouch:self];

        

    }


}

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

{

    //当时customView在触摸结束时,通知代理执行相应的代理.实现相应的某种操作.

    if ([_delegate respondsToSelector:@selector(customViewEndTouch:)])

    {

        

        

        [_delegate customViewEndTouch:self];

        

        

    }

    



}



/*

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

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/


@end

打开 RootViewController.h文件:

//

//  RootViewController.h

//  Delegate

//

//  Created by feizhu on 14-9-19.

//  Copyright (c) 2014 www.feizhu.com. All rights reserved.

//


#import


//遵守协议

#import "CustomView.h"

@interface RootViewController : UIViewController<CustomViewDelegate>


@end


RootViewController.m文件:

//

//  RootViewController.m

//  Delegate

//

//  Created by feizhu on 14-9-19.

//  Copyright (c) 2014 feizhu. All rights reserved.

//


#import "RootViewController.h"

#import "CustomView.h"

@interface RootViewController ()


@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.

    CustomView *view = [[CustomView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];

    view.backgroundColor = [UIColor grayColor];

    [self.view addSubview:view];

    [view release];

    //设置customView协议

    view.delegate = self;

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.


}


//customView开始被触摸时

- (void)customViewBeganTouch:(CustomView *)customView

{

    NSLog(@"%s",__FUNCTION__);

    customView.backgroundColor = [UIColor redColor];


}


//customView被触摸的过程中

- (void)customViewMovedTouch:(CustomView *)customView

{

     NSLog(@"%s",__FUNCTION__);

    customView.backgroundColor = [UIColor yellowColor];


}

//customView结束被触摸时

- (void)customViewEndTouch:(CustomView *)customView

{

     NSLog(@"%s",__FUNCTION__);

    customView.backgroundColor = [UIColor blueColor];


}



/*

#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






你可能感兴趣的:(iOS)