1.实现的是微信的摇一摇
2.实现的是View随着触摸能够在视图中进行移动,还有是通过触碰实现颜色变换,一碰颜色就会得到改变
//
// AppDelegate.m
// UI04_Touch
//
// Created by dllo on 15/8/3.
// Copyright (c) 2015年 zhozhicheng. 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
//
// MyView.m
// UI04_Touch
//
// Created by dllo on 15/8/3.
// Copyright (c) 2015年 zhozhicheng. 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:1];
//集合中元素个数
NSLog(@"%ld",touches.count);
//集合里有一个触摸类的对象
UITouch *touch=[touches anyObject];
//通过触摸对象获取相应视图的当前位置
self.startPoint=[touch locationInView:self];
NSLog(@"%g",self.startPoint.x);
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(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);
}
@end
//
// MainViewController.m
// UI04_Touch
//
// Created by dllo on 15/8/3.
// Copyright (c) 2015年 cml. 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];
}
//-(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, 500, 150, 40)];
self.myTextField.layer.cornerRadius =10;
self.myTextField.layer.borderWidth =2;
[self.view addSubview:_myTextField];
[_myTextField release];
// // 创建一个MyView;
// MyView *myView=[[MyView alloc] initWithFrame:CGRectMake(100, 200, 150, 40)];
// myView.backgroundColor =[UIColor redColor];
// [self.view addSubview:myView];
// [myView release];
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
view.backgroundColor =[UIColor yellowColor];
[self.view addSubview:view];
[view release];
UIButton *button1=[UIButton buttonWithType:UIButtonTypeSystem];
button1.frame=CGRectMake(0, 0, 100, 100);
button1.backgroundColor=[UIColor cyanColor];
button1.layer.borderWidth=1;
button1.layer.cornerRadius=10;
[view addSubview:button1];
[button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2=[UIButton buttonWithType:UIButtonTypeSystem];
button2.frame=CGRectMake(150,150, 100, 100);
button2.backgroundColor=[UIColor blackColor];
button2.layer.borderWidth=1;
button2.layer.cornerRadius=10;
[view addSubview:button2];
[button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸开始");
// 点击空白处,回收键盘
[self.myTextField resignFirstResponder];
}
-(void)click:(UIButton *)button
{
NSLog(@"测试是否执行");
[self.myTextField becomeFirstResponder];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸结束");
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸被取消");
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"触摸移动");
}
//hardview中 shake Gestrue 模拟摇一摇
// 摇一摇
-(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(@"摇一摇被取消");
}
// hardview 中Simulate Memory Warning 模拟内存管理
- (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
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-02 23:35:20