UITouch 点击事件

#import "AppDelegate.h"

#import "ViewController.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];

    

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

    self.window.rootViewController = rootVC;

    [rootVC release];

    

    [self.window makeKeyAndVisible];

    return YES;

}

 

 

 

 

#import "ViewController.h"

#import "TView.h"

@interface ViewController ()

 

@end

 

@implementation ViewController

 

 

 

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    TView *view1 = [[TView alloc]initWithTarget:self Action:@selector(changeCount:)];

    view1.frame = CGRectMake(40, 100, 100, 100);

    view1.backgroundColor = [UIColor colorWithHue:arc4random() % 256 / 255.0 saturation:arc4random() % 256 / 250.0 brightness:arc4random() % 256 /250.0 alpha:1.0];

    [self.view addSubview:view1];

    [view1 release];

    

    TView *view2 = [[TView alloc]initWithTarget:self Action:@selector(changeFrame:)];

    view2.frame = CGRectMake(200, 100, 100, 100);

    view2.backgroundColor = [UIColor colorWithHue:arc4random() % 256 / 255.0 saturation:arc4random() % 256 / 250.0 brightness:arc4random() % 256 /250.0 alpha:1.0];

    [self.view addSubview:view2];

    [view2 release];

    

    TView *view3 = [[TView alloc]initWithTarget:self Action:@selector(changeSize:)];

    view3.frame = CGRectMake(40, 300, 100, 100);

    view3.backgroundColor = [UIColor colorWithHue:arc4random() % 256 / 255.0 saturation:arc4random() % 256 / 250.0 brightness:arc4random() % 256 /250.0 alpha:1.0];

    [self.view addSubview:view3];

    [view3 release];

    

}

 

- (void)changeCount:(UIView *)view

{

    view.backgroundColor = [UIColor colorWithHue:arc4random() % 256 / 255.0 saturation:arc4random() % 256 / 250.0 brightness:arc4random() % 256 /250.0 alpha:1.0];

}

 

- (void)changeFrame:(UIView *)view

{

    view.center = CGPointMake(arc4random() % 320, arc4random() % 480);

 

}

 

- (void)changeSize:(UIView *)view

{

    

    view.frame = CGRectMake(40, 300, arc4random() % 320 + 20, arc4random() % 480 + 20);

 

}

 

 

 

 

#import <UIKit/UIKit.h>

 

@interface TView : UIView

{

    id _target;

    SEL _action;

}

 

- (id)initWithTarget:(id)target Action:(SEL)action;

 

@end

 

 

 

 

#import "TView.h"

 

@implementation TView

 

- (id)initWithTarget:(id)target Action:(SEL)action

{

    self = [super init];

    if (self) {

        _target = target;

        _action = action;

    }

    return self;

}

 

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

{

    [_target performSelector:_action withObject:self];

}

 

 

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

{

    

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self.superview];

    CGPoint point1 = [touch previousLocationInView:self.superview];

    CGPoint center = self.center;

    CGPoint point2 = CGPointMake(point.x - point1.x, point.y - point1.y);

    self.center = CGPointMake(center.x + point2.x, center.y + point2.y);

 

   

}

 

-(void)touchesEnded:(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

 

原文地址:https://www.cnblogs.com/jx451578429/p/4755981.html