UI一揽子计划 4 (Touch 、摇一摇、响应者链)

一、touch
- ( void )touchesBegan:( NSSet *)touches withEvent:( UIEvent *)event  // 开始触摸,就只被执行一次
{
  //  NSLog(@" 开始触摸 .");
   
/**
     *  2015-08-26 10:32:10.933 LessonUITouch[1650:58351]
     <UITouch: 0x7fac30f8f230>
     phase: Began tap count: 1
     window: <UIWindow: 0x7fac30f905b0;
        frame = (0 0; 375 667);
        gestureRecognizers = <NSArray: 0x7fac30f91800>;
        layer = <UIWindowLayer: 0x7fac30f90540>
     >
     view: <TouchView: 0x7fac30f948a0;
        frame = (100 100; 100 100);
        layer = <CALayer: 0x7fac30f8f040>
     >
    
--<<<<<<<<<<------------------下面的信息相对重要-------->>>>>>>>>>>-
    
     location in window: {121.5, 158.5}     // 
当前光标在 window 上的位置
     previous location in window: {121.5, 158.5}  //
拖拽前上一个点的位置   相对于 window
     location in view: {21.5, 58.5}  //
当前光标在 touchView 上的位置
     previous location in view: {21.5, 58.5} //
拖拽前上一个点的位置   相对于 touchView
     */

   
// 保存了触摸时候手指的信息
   
UITouch *touch = [touches anyObject ];
  
// NSLog(@"%@", touch);
   
// 取出一个点
   
CGPoint p1 = [touch locationInView : self ];
   
self . startPoint = p1;
 
//  CGPoint p2 = [touch previousLocationInView:self];
  
// NSLog(@"%@", NSStringFromCGPoint(p2));
 
//  NSLog(@"%@", NSStringFromCGPoint(p1));
   
self . backgroundColor = [ UIColor colorWithRed : arc4random ()% 256 / 255.0 green : arc4random ()% 256 / 255.0 blue : arc4random ()% 256 / 255.0 alpha : 1.000 ];
   
}

- (
void )touchesCancelled:( NSSet *)touches withEvent:( UIEvent *)event
{
   
NSLog ( @" 扫兴,抚摸中断了 ..." );
}
- (
void )touchesEnded:( NSSet *)touches withEvent:( UIEvent *)event
{
   
UITouch *touch = [touches anyObject ];
   
CGPoint ending = [touch locationInView : self ];
   
CGFloat dx = ending. x - self . startPoint . x ;
   
if (dx > 10 ) {
       
NSLog ( @" 右滑 " );
    }
else if (dx < - 10 ) {
       
NSLog ( @" 左滑 " );
    }
   
NSLog ( @"%f" , dx);
   
NSLog ( @" 摸够了 ..." );
}
- (
void )touchesMoved:( NSSet *)touches withEvent:( UIEvent *)event
{
  
// NSLog(@" 抚摸中 ...");
   
UITouch *touch = [touches anyObject ];
   
CGPoint p1 = [touch locationInView : self ];
   
CGPoint p2 = [touch previousLocationInView : self ];
   
// 计算偏移的距离
   
CGFloat dx = p1. x - p2. x ;
   
CGFloat dy = p1. y - p2. y ;
   
// 原来中心点的坐标加上偏移量 就得到了新的位置
 
//  CGPoint p3 = {self.center.x + dx, self.center.y + dy};
 
//   NSLog(@"%@", NSStringFromCGPoint(p2));
 
//   NSLog(@"%@", NSStringFromCGPoint(p1));
 
//self.center = p3;
  
// self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.000];
  }

二、摇一摇
加载视图   加载的是自带的 self.view
- ( void )loadView
{
    [
super loadView ]; // 如果要重写这个方法,就必须要写这个方法。如果没有这个方法 我们就不能使用它 因为它是一个空的视图
}
- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
self . view . backgroundColor = [ UIColor colorWithRed : 0.315 green : 0.867 blue : 1.000 alpha : 1.000 ];
 
   
// Do any additional setup after loading the view.
}

- (
void )motionBegan:( UIEventSubtype )motion withEvent:( UIEvent *)event
{
   
NSLog ( @" 开始摇 " );
}
- (
void )motionEnded:( UIEventSubtype )motion withEvent:( UIEvent *)event
{
  
// SecondViewController *sec = [[SecondViewController alloc]init];
   
WeChatLoginViewController *weChat = [[ WeChatLoginViewController alloc ] init ];
    [
self presentViewController :weChat animated : YES completion : nil ];
    [weChat
release ];
   
   
NSLog ( @" 摇完了 " );
}
- (
void )motionCancelled:( UIEventSubtype )motion withEvent:( UIEvent *)event
{
   
NSLog ( @" 取消 " );
}
三、响应者链
 /**
     * 
响应者链
       
响应者链分为两个过程(查找响应)
            1.
查找过程
               
先找到是哪个应用程序->找是哪个window ->哪个控制器(页面) -> self.view  ->所有的子视图 找到为止
    
            2.
响应过程
               
如何才能处理事件?
                   
能捕捉到点击事件,即实现了touchBegan等方法
                   
看所有子视图能不能处理  -> self.view ->所在控制器->所在window ->所属应用程序
                   
如果没有视图处理这个事件,那么这个点击事件就被遗弃
     */

   
//阻断了查询的过程,不让进行查询的过程,都不会走到响应的步骤。
   
//直译:用户交互影响授权 系统默认是NO(阻断查询过程(把交互关闭))/  YES(不阻断查询过程(把交互打开))
   
self.window.userInteractionEnabled=YES;
   
WeChatLoginViewController*rootVC = [[WeChatLoginViewControlleralloc]init];
   
self.window.rootViewController= rootVC;
    [rootVC release];

1. 姚贝娜的照片。
     UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"123.jpg" ]];
     imageView.frame = CGRectMake(0, 0, 375, 667);
     [self.window addSubview:imageView];
     [imageView release];
     UIButton *button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
     [button1 addTarget:self action:@selector(action:) forControlEvents:(UIControlEventTouchUpInside)];
     [button1 setTitle:@"
点我 " forState:(UIControlStateNormal)];
     [button1 setTitle:@"
点我 " forState:(UIControlStateHighlighted)];
     [button1 setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted)];
     button1.frame = CGRectMake(70, 180, 200, 200);
     [imageView addSubview:button1];
     imageView.userInteractionEnabled = YES;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值