进击的KFC:UI(四)实现划屏效果,用View实现Button的效果

实现划屏效果:
UITouch类:保存手指信息(触摸的点)
开始触摸方法:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     UITouch *touch = [touches anyObject];
     // 取出当前触摸的点:
     CGPoint p1 = [touch locationInView:self];
     CGPoint p2 = [touch previousLocationInView];
}

// 只要你不松手,就一直调用这个方法
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    // 获取touch位置处的信息
    UITouch *touch  = [touches anyObject];
    // 取出当前的点
    CGPoint p1 = [touch locationInView:self];   // self 就是TouchView本体
    // 取出当前点的上一个点,
    CGPoint p2 = [touch previousLocationInView:self];
    //计算x轴的偏移量,
    CGFloat x = p1.x - p2.x;
    // 计算y轴的偏移量
    CGFloat y = p1.y - p2.y;
    // 动态改变视图中原来的中心点,这样写,不会跳顿
    self.center = CGPointMake(x + self.center.x, y + self.center.y);

    NSLog(@"移动中的point:%@",NSStringFromCGPoint(p1));

    // 三原色 计算所占的比例来调整颜色
    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];

    NSLog(@"触摸中........");


}

  /*
     响应者链分为两个过程
     1.查询过程
     当你点击屏幕
       先定位到 应用程序Application -> window -> ViewController -> self.view -> view上的子视图 一一查找,直到定位到被点击的子视图 查询过程结束

     2.响应过程
      首先,看本视图,能不能处理事件(实现了touchBegin等方法 就叫做可以处理事件) -> 父视图 -> 一层一层往下看,能不能处理,直到window 如果都不能处理该次点击事件 被遗弃(无效点击)



     阻断响应者链 : 响应者链可以被打断。⽆法完成检测查询过程
     注意:UILabel UIImageView的交互 默认是关闭的
// 关闭button的交互 (阻断的是查询的过程)
// button.userInteractionEnabled = NO;
     */
用presentViewController,dismissViewController 方法来跳转页面. 模态视图动画是从下面跳出来,常用于跳出一个登陆界面,
晃动方法:motionBegan:...等四个方法

用继承UIView的ButtonView的类来实现UIButton的添加方法的功能!!
1.新建一个ButtonView,继承UIView
3.在ButtonView.h中声明属性
// 为了方便下面使用
// 模仿按钮的两个属性
// button addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>
@Proterty (strong,nonatomic)id target;
@proterty (assign,nonatomic)SEL action;
- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action;
3.在ButtonView.h里面实现自定义初始化方法,在初始化的时候就给id target 和 SEL action 赋值
- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
     self = [super initWithFrame:frame];
     if(self){
          // 初始化的时候,给属性进行赋值
          self.target = target;
          self.action = action;
   }
  return self;
}
// 触摸的四个方法,我们主要用到第三个:触摸结束时:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"点击");
// 让self.target 这个 对象 去调用 action方法
// 让一个 对象 去调用这个对象类里的方法
// Object 是可携带参数:Controller里面的buttonView的点击方法就传入什么类型的参数
 self.target performSelector:self.action withObject:self];

}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
// 然后在RootViewController 根视图控制器里面调用
先创建一个ButtonView
ButtonView *buttonView =  [[ButtonView alloc] initWithFrame:CGRectMake(100, 100, 100, 50) target:self action: @selector(buttonViewClick:)];
 buttonView.background =[UIColor redColor];
 [self.View addSubView:buttonView];
 [buttonView release];

// 实现buttonViewClick方法
- (void)buttonViewClick:(ButtonView *)buttonview
{
      // 简单操作:该变颜色:
      // 改变我们点击的buttonView的颜色,就是改变输入的参数buttonview的背景颜色
   buttonview.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random() %256/ 255.0 blue:arc4random() %256/ 255.0 alpha:1];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值