UI_Touch

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

AppDelegate.m

#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]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];

    RootViewController *rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = rootVC;
    [rootVC release];

    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
#import "MyView.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

    //  创建一个myView
    MyView *firstView = [[MyView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    firstView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:firstView];
    [firstView release];

    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeSystem];
    firstButton.frame = CGRectMake(0, 0, 100, 100);
    [firstView addSubview:firstButton];
    firstButton.backgroundColor = [UIColor redColor];
    [firstButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

//    UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    secondButton.frame = CGRectMake(150, 150, 100, 100);
    UITextField *secondButton = [[UITextField alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
    [firstView addSubview:secondButton];
    [secondButton release];
    secondButton.backgroundColor = [UIColor cyanColor];
    [secondButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    //  成为第一响应者, 相应的就会弹出键盘  点击空白回收键盘
    [secondButton becomeFirstResponder];
    secondButton.tag = 1000;


    UISwitch *switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(150, 150, 50, 30)];
    [self.view addSubview:switchButton];
    [switchButton release];
    switchButton.backgroundColor = [UIColor whiteColor];
//    switchButton.tintColor = [UIColor blackColor];
//    switchButton.onTintColor = [UIColor cyanColor];
//    switchButton.thumbTintColor = [UIColor lightGrayColor];
//    switchButton.onImage = [UIImage imageNamed:@"check.png"];
    switchButton.layer.cornerRadius = 15;
    [switchButton sizeToFit];


}

- (void)click:(UIButton *)button {
    NSLog(@"我是来测试的");
}


#warning 在继承和使用父类的方法的时候, 一般会先用super去调用父类相应的方法, 目的是为了保证方法的原功能不变, 在此基础上再添加我们自己的功能
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    //  通过tag值先找textfield
    UITextField *textField = (UITextField *)[self.view viewWithTag:1000];
    [textField resignFirstResponder];

    NSLog(@"触摸开始");
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    NSLog(@"触摸移动");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    NSLog(@"触摸结束");
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    NSLog(@"触摸取消");
}
//  摇一摇
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionBegan:motion withEvent:event];
    NSLog(@"晃动");
    //  设置随机背景颜色
    self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionCancelled:motion withEvent:event];
    NSLog(@"晃动取消");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionEnded:motion withEvent:event];
    NSLog(@"晃动结束");
}

MyView.h

#import <UIKit/UIKit.h>

@interface MyView : UIView

@end

MyView.m

#import "MyView.h"

@interface MyView ()

@property(nonatomic, assign)CGPoint startPoint;


@end


@implementation MyView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    //  集合里保存的是触摸时候产生的触摸对象
    NSLog(@"%ld", touches.count);
    //  触摸对象获取到
    UITouch *touch = [touches anyObject];
    //  根据触摸对象, 获取开始的坐标位置
    self.startPoint = [touch locationInView:self];

}

//  移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    //  获取触摸对象
    UITouch *touch = [touches anyObject];
    //  获取移动的坐标位置
    CGPoint newPoint = [touch locationInView:self];
    //  计算两个坐标产生的变化
    CGFloat dx = newPoint.x - self.startPoint.x;
    CGFloat dy = newPoint.y - self.startPoint.y;
    //  改变自身的变化
    self.center = CGPointMake(self.center.x + dx, self.center.y + dy);
    //  设置随机背景颜色
    self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];

}

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

}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值