UIView 拖动窗口代码

1.坐标系在self.view

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

   if ([touches count] >=1)

    {

       UITouch *touch = [touches anyObject];

        

//         if ([touch view] == self.videoCallSmallView)

        {

       CGPoint point = [touch locationInView:self.view];

        

        orignalPoint =CGPointMake(self.videoCallSmallView.center.x - point.x,self.videoCallSmallView.center.y- point.y);

         }

    }

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    

   if ([touches count] >=1)

    {

       UITouch *touch = [touches anyObject];

        

//        if ([touch view] == self.videoCallSmallView)

        {

           CGPoint point = [touch locationInView:self.view];

           self.videoCallSmallView.center =CGPointMake(point.x +orignalPoint.x, point.y +orignalPoint.y);

        }

    }

}


2.坐标系在拖动view里

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

   if ([touches count] >=1)

    {

       UITouch *touch = [touches anyObject];

        

       if([touch view].tag == VIDEOCALLSMALLVIEW_TAG)//拖动点在小窗口内

        {

            beginPoint_ = [touchlocationInView:self.videoCallSmallView];

        }

    }

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    

   if ([touches count] >=1)

    {

       UITouch *touch = [touches anyObject];

        

       if([touch view].tag == VIDEOCALLSMALLVIEW_TAG)//拖动点在小窗口内

        {

           CGPoint currentPosition = [touch locationInView:self.videoCallSmallView];

            

           //偏移量

           float offsetX = currentPosition.x -beginPoint_.x;

           float offsetY = currentPosition.y -beginPoint_.y;

            self.videoCallSmallView.center =CGPointMake(self.videoCallSmallView.center.x + offsetX, self.videoCallSmallView.center.y + offsetY);

            

           //防止拖出屏幕

           //x轴左右极限坐标

            if (self.videoCallSmallView.center.x > (self.videoCallSmallView.superview.frame.size.width - self.videoCallSmallView.frame.size.width/2))

            {

                CGFloat x =self.videoCallSmallView.superview.frame.size.width - self.videoCallSmallView.frame.size.width/2;

               self.videoCallSmallView.center =CGPointMake(x, self.videoCallSmallView.center.y + offsetY);

            }

            elseif (self.videoCallSmallView.center.x < self.videoCallSmallView.frame.size.width/2)

            {

               CGFloat x = self.videoCallSmallView.frame.size.width/2;

               self.videoCallSmallView.center =CGPointMake(x, self.videoCallSmallView.center.y + offsetY);

            }

            

           //y轴上下极限坐标

            if (self.videoCallSmallView.center.y > (self.videoCallSmallView.superview.frame.size.height-self.videoCallSmallView.frame.size.height/2))

            {

               CGFloat x = self.videoCallSmallView.center.x;

                CGFloat y =self.videoCallSmallView.superview.frame.size.height-self.videoCallSmallView.frame.size.height/2;

               self.videoCallSmallView.center =CGPointMake(x, y);

            }

            elseif (self.videoCallSmallView.center.y <= self.videoCallSmallView.frame.size.height/2)

            {

               CGFloat x = self.videoCallSmallView.center.x;

               CGFloat y = self.videoCallSmallView.frame.size.height/2;

               self.videoCallSmallView.center =CGPointMake(x, y);

            }

        }

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值