iOS UI04_Touch

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-08-25 15:52:53

iOS UI04_Touch的相关文章

iOS -- SKSpriteNode类

SKSpriteNode类 继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKSpriteNode.h 参考指南 Sprite Kit Progamming Guide 概览 重要提示:这是一个初步的API或者开发技术

使用fruitstrap实现命令行将IPA包安装到iOS设备上

Requirements Mac OS X. Tested on Snow Leopard only. You need to have a valid iPhone development certificate installed. Xcode must be installed, along with the SDK for your iOS version. Usage fruitstrap [-d] -b <app> [device_id] Optional -d flag laun

iOS -- SKScene类

SKScene类 继承自 SKEffectNode:SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKScene.h 参考指南 Sprite Kit Progamming Guide 概览 重要提示:这是一个初步的API或者开

iOS -- SKPhysicsWorld类

SKPhysicsWorld类 继承自 NSObject 符合 NSCodingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKPhysicsWorld.h 参考指南 Sprite Kit Progamming Guide 概览 重要提示:这是一个初步的API或者开发技术文档.虽然已经审阅了本文档的技术准确性,但是它不是最终的版本.本机密信息仅适用于

iOS证书说明和发布

1.首先通过钥匙串访问——证书助理——从证书颁发机构请求证书——填写证书信息(邮箱,常用名称,存储到磁盘)——存储为(自定义名称.certSigningReuqest,简称CSR文件,只是为了提交到苹果开发者账号中,然后就没用了)到本地 2.苹果开发者账号中,创建证书(Development和Production)——上传CSR文件——下载证书运行 ( xxx.cer文件) 注意:只有在当前电脑中生成本地生成证书,上传到苹果开发账号,然后下载cer文件运行后,钥匙串中才有证书以及对应的秘钥 如果

iOS开发——项目实战总结&amp;UITableView性能优化与卡顿问题

UITableView性能优化与卡顿问题 1.最常用的就是cell的重用, 注册重用标识符 如果不重用cell时,每当一个cell显示到屏幕上时,就会重新创建一个新的cell 如果有很多数据的时候,就会堆积很多cell.如果重用cell,为cell创建一个ID 每当需要显示cell 的时候,都会先去缓冲池中寻找可循环利用的cell,如果没有再重新创建cell 2.避免cell的重新布局 cell的布局填充等操作 比较耗时,一般创建时就布局好 如可以将cell单独放到一个自定义类,初始化时就布局好

解决ios下的微信打开的页面背景音乐无法自动播放

后面的项目发现,还有两个坑,需要注意下: ·本文的解决方案的核心是利用了 微信/易信 在ready的时候会有个 WeixinJSBridgeReady/YixinJSBridgeReady事件,通过监听这个事件来触发的.那有个坑就是 如果微信已经ready了,但还没执行到你监听这个ready事件的代码,那么你的监听是没用的,所以最理想的情况是,监听的js放在head前面(放在css外链之前),确保最新执行,切记!切记!. ·另一个坑就是,本文的解决方案只适合一开始就播放的背景音乐.如果你是做那种

iOS程序执行顺序和UIViewController 的生命周期(整理)

说明:此文是自己的总结笔记,主要参考: iOS程序的启动执行顺序 AppDelegate 及 UIViewController 的生命周期 UIView的生命周期 言叶之庭.jpeg 一. iOS程序的启动执行顺序 程序启动顺序图 iOS启动原理图.png 具体执行流程 程序入口进入main函数,设置AppDelegate称为函数的代理 程序完成加载[AppDelegate application:didFinishLaunchingWithOptions:] 创建window窗口 程序被激活[

iOS库--.a与.framework

一.什么是库? 库是共享程序代码的方式,一般分为静态库和动态库. 二.静态库与动态库的区别? 静态库:链接时完整地拷贝至可执行文件中,被多次使用就有多份冗余拷贝. 动态库:链接时不复制,程序运行时由系统动态加载到内存,供程序调用,系统只加载一次,多个程序共用,节省内存. 三.iOS里静态库形式? .a和.framework 四.iOS里动态库形式? .dylib和.framework 五.framework为什么既是静态库又是动态库? 系统的.framework是动态库,我们自己建立的.fram