当前位置: 首页 > 工具软件 > YDUI-Touch > 使用案例 >

UI04_Touch

越风史
2023-12-01
//
//  AppDelegate.h
//  UI04_Touch
//
//  Created by dllo on 15/8/3.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
//
//  AppDelegate.m
//  UI04_Touch
//
//  Created by dllo on 15/8/3.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.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];
    
    // 设置根视图控制器
    MainViewController *mainVC = [[MainViewController alloc] init];
    self.window.rootViewController = mainVC;
    [mainVC 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

//
//  MainViewController.m
//  UI04_Touch
//
//  Created by dllo on 15/8/3.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "MainViewController.h"
#import "MyView.h"
@interface MainViewController ()

// TextField
@property(nonatomic, retain)UITextField *myTextField;

@end

@implementation MainViewController

- (void)dealloc
{
    [_myTextField release];
    [super dealloc];
}


// 初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil
            ];
    if (self) {
        
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    /// ViewController中初始化方法,loadView,viewdidLoad只会运行一次,但是viewappear只要视图显示,就会运行一次
    
    self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
    self.myTextField.layer.cornerRadius = 10;
    self.myTextField.layer.borderWidth = 1;
    [self.view addSubview:self.myTextField];
    [_myTextField release];
    
    // 创建一个MyView
//    MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(100, 200, 200, 100)];
//    myView.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:myView];
//    [myView release];
    
    // 创建一个普通的View
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 150, 200, 200)];
    view.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:view];
    [view release];
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(0, 0, 100, 100);
    button1.backgroundColor = [UIColor redColor];
    [view addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(150, 150, 100, 100);
    button2.backgroundColor = [UIColor cyanColor];
    [view addSubview:button2];
    [button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    


}

- (void)click:(UIButton *)button
{
    NSLog(@"测试点击方法是否执行");
    
    [self.myTextField becomeFirstResponder];
}

/// 触摸
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸开始");
    // 点击空白处,回收键盘
    [self.myTextField resignFirstResponder];
}

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

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

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"触摸取消");
}

// 摇一摇
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇开始");
    self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇结束");
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"摇一摇被取消");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    NSLog(@"内存警告!");
}

/*
#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

//
//  MyView.m
//  UI04_Touch
//
//  Created by dllo on 15/8/3.
//  Copyright (c) 2015年 Clare. All rights reserved.
//

#import "MyView.h"

// 补上延展部分
@interface MyView()

// 用来记录视图的开始坐标
@property(nonatomic, assign)CGPoint startPoint;


@end
@implementation MyView

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    self.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];
    // 集合里元素个数
    NSLog(@"%ld", touches.count);
    
    // 集合里有一个触摸类的对象
    UITouch *touch = [touches anyObject];
    
    // 通过触摸对象获取相应的视图的当前位置
    self.startPoint = [touch locationInView:self];
    
    NSLog(@"%g", self.startPoint.x);
    NSLog(@"%g", self.startPoint.y);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 通过移动,找到变化,然后让MyView也进行相应的调整,从而实现视图随手移动的效果
    // 获取触摸的对象
    UITouch *touch = [touches anyObject];
    // 获取移动之后的坐标
    CGPoint movePoint = [touch locationInView:self];
    
    // 坐标的变化
    CGFloat dx = movePoint.x - self.startPoint.x;
    CGFloat dy = movePoint.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:0.7];
}

- (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


 类似资料:

相关阅读

相关文章

相关问答