移动uiView 缩放uiView+一个View实现不同子view的功能

    //移动uiView
    UITouch *touch1=[touches anyObject];
    CGPoint onePoint=[touch1 locationInView:self.window];
    CGPoint newOnePoint=[touch1 previousLocationInView:self.window];
    CGFloat distanceX=onePoint.x-newOnePoint.x;
    CGFloat distanceY=onePoint.y-newOnePoint.y;
    self.center=CGPointMake(self.center.x+distanceX,self.center.y+distanceY);
    //显示uiview移动的方向
    if (onePoint.x<newOnePoint.x) {
        NSLog(@"向左移动");
    }else if (onePoint.x>newOnePoint.x){
        NSLog(@"向右移动");
    }else if (onePoint.y>newOnePoint.y){
        NSLog(@"向下移动");
    }else if(onePoint.y<newOnePoint.y){
        NSLog(@"向上移动");
    }
    //缩放uiView
    if (touches.count>=2) {
        //获取每一个touches的x,y值
        NSArray *array=[touches allObjects];
        CGPoint point1=[[array objectAtIndex:0] locationInView:self];
        CGPoint point2=[[array objectAtIndex:1] locationInView:self];
        CGPoint newPoint1=[[array objectAtIndex:0] previousLocationInView:self];
        CGPoint newPoint2=[[array objectAtIndex:1] previousLocationInView:self];
        //判断是不是同一个点并根据center缩放大小
        if (point1.x!=point2.x && point1.y!=point2.y) {
            CGFloat num=sqrt(((point2.x-point1.x)*(point2.x-point1.x))+((point2.y-point1.y)*(point2.y-point1.y)));
            CGFloat newNum=sqrt(((newPoint2.x-newPoint1.x)*(newPoint2.x-newPoint1.x))+((newPoint2.y-newPoint1.y)*(newPoint2.y-newPoint1.y)));
            self.bounds=CGRectMake(0, 0,self.bounds.size.width*(num/newNum),self.bounds.size.height*(num/newNum));
        }
    }


RootViewController
    DragView *dragView=[[DragView alloc]initWithFrame:CGRectMake(30, 30, 130, 140)];
    dragView.backgroundColor=[UIColor redColor];
    [self.view addSubview:dragView];





//4.创建一个TouchView(kongjian),创建TouchView的3个实例,touchView1
被点击之后,touchView换背景颜色.tuochView2被点击后换位置(随机cengter),touchView3被点击后,打印一句话





    if ([self viewWithTag:100]) {
        self.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green: arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    }else if ([self viewWithTag:200])
    {
        self.center=CGPointMake(arc4random()%100+30, arc4random()%400+40);
    }else if ([self viewWithTag:300])
    {
        NSLog(@"minamima");
    }


RootViewContrller文件

    TouchView *touchViewColor=[[TouchView alloc]initWithFrame:CGRectMake(30, 30, 60, 30)];
    touchViewColor.backgroundColor=[UIColor redColor];
    [self.view addSubview:touchViewColor];
    touchViewColor.tag=100;
    

    TouchView *touchViewText=[[TouchView alloc]initWithFrame:CGRectMake(30, 200, 60, 30)];
    touchViewText.backgroundColor=[UIColor redColor];
    [self.view addSubview:touchViewText];
    touchViewText.tag=300;
    
    TouchView *touchViewCenter=[[TouchView alloc]initWithFrame:CGRectMake(30, 120, 60, 30)];
    touchViewCenter.backgroundColor=[UIColor redColor];
    [self.view addSubview:touchViewCenter];
    touchViewCenter.tag=200;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值