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);
}
}
}
}