Iphone 如何将旋转的UIView保存在另一个UIView中?(不能使用框架)

Iphone 如何将旋转的UIView保存在另一个UIView中?(不能使用框架),iphone,objective-c,cocoa-touch,ipad,uikit,Iphone,Objective C,Cocoa Touch,Ipad,Uikit,我希望在另一个视图中有一个可移动/可缩放/可旋转的视图。内部视图可以超出外部视图的框架,但我想将部分视图保留在外部视图中,这样内部视图就不会丢失 我简化了这个xcode项目中的问题 如果内部视图只是可移动和可伸缩的,那么问题就已经解决了,但是当内部视图旋转此解决方案时,这并不好,因为框架包含视图,但它不是视图本身 对内部视图执行的每次转换我都使用此函数验证新视图位置,如果无效,我将还原上一次转换(这是可移动视图代码) -(BOOL)validInset{ CGRect outerLimit=CG

我希望在另一个视图中有一个可移动/可缩放/可旋转的视图。内部视图可以超出外部视图的框架,但我想将部分视图保留在外部视图中,这样内部视图就不会丢失

我简化了这个xcode项目中的问题

如果内部视图只是可移动和可伸缩的,那么问题就已经解决了,但是当内部视图旋转此解决方案时,这并不好,因为框架包含视图,但它不是视图本身

对内部视图执行的每次转换我都使用此函数验证新视图位置,如果无效,我将还原上一次转换(这是可移动视图代码)

-(BOOL)validInset{
CGRect outerLimit=CGRectMake(0,0,self.superview.frame.size.width,self.superview.frame.size.height);
CGRect intersectionRect=CGRectIntersection(self.frame,outerLimit);
NSLog(@“self.frame:%f,%f,%f,%f”,self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
NSLog(@“outer.frame:%f,%f,%f,%f”,outerLimit.origin.x,outerLimit.origin.y,outerLimit.size.width,outerLimit.size.height);
NSLog(@“中间帧:%f,%f,%f,%f”,intersectionRect.origin.x,intersectionRect.origin.y,intersectionRect.size.width,intersectionRect.size.height);
NSLog(@“========================================”);
if(CGRectIsNull(intersectionRect)||
intersectionRect.size.width<插图||
intersectionRect.size.height<插图){
返回否;
}
否则{
返回YES;
}
}
问题是,当第一个视图旋转一些(例如45度)并拖动到某个角落时,如何确保内部视图不会丢失在外部视图后面?

我想在外部视图中保留一些像素,因为内部视图可以比外部视图更大(缩放)

我建议你下载并运行这个项目来更好地理解这个问题,仅仅阅读本文是很难理解的


谢谢大家!

如您所述,只有内部视图未旋转时,您的方法才能正常工作

我所知道的解决问题的最简单方法就是简单地测试一个视图的中心或顶点是否在另一个视图内

-(BOOL) validInset {
    // Get the vertices of A (outerLimit)
    CGRect outerLimit = self.superview.bounds;
    CGPoint pointA[] = {
        CGPointMake(outerLimit.origin.x,   outerLimit.origin.y    ),
        CGPointMake(outerLimit.size.width, outerLimit.origin.y    ),
        CGPointMake(outerLimit.size.width, outerLimit.size.height ),
        CGPointMake(outerLimit.origin.x,   outerLimit.size.height )};
    // Adjust outerLimit's borders
    pointA[0].x += INSET, pointA[0].y += INSET;
    pointA[1].x -= INSET, pointA[1].y += INSET;
    pointA[2].x -= INSET, pointA[2].y -= INSET;
    pointA[3].x += INSET, pointA[3].y -= INSET;
    // Get the vertices of B (innerView)
    CGRect innerView = self.bounds;
    CGPoint pointB[] = {
        CGPointMake(innerView.origin.x,   innerView.origin.y    ),
        CGPointMake(innerView.size.width, innerView.origin.y    ),
        CGPointMake(innerView.size.width, innerView.size.height ),
        CGPointMake(innerView.origin.x,   innerView.size.height )};
    // Test if the center of B is inside A or vice versa
    CGPoint center, converted;
    center = CGPointMake(pointB[0].x + (pointB[1].x - pointB[0].x)/2, pointB[0].y + (pointB[2].y - pointB[0].y)/2);
    if( converted = [self convertPoint:center toView:self.superview],
        converted.x >= pointA[0].x && 
        converted.x <= pointA[1].x &&
        converted.y >= pointA[0].y &&
        converted.y <= pointA[2].y ) return YES;
    center = CGPointMake(pointA[0].x + (pointA[1].x - pointA[0].x)/2, pointA[0].y + (pointA[2].y - pointA[0].y)/2);
    if( converted = [self convertPoint:center toView:self.superview],
        converted.x >= pointA[0].x && 
        converted.x <= pointA[1].x &&
        converted.y >= pointA[0].y &&
        converted.y <= pointA[2].y ) return YES;
    // Test if vertices of B are inside A or vice versa
    for (int i = 0; i < 4; i++) {
        if( converted = [self convertPoint:pointB[i] toView:self.superview],
            converted.x >= pointA[0].x && 
            converted.x <= pointA[1].x &&
            converted.y >= pointA[0].y &&
            converted.y <= pointA[2].y ) return YES;
        if( converted = [self.superview convertPoint:pointA[i] toView:self],
            converted.x >= pointB[0].x && 
            converted.x <= pointB[1].x &&
            converted.y >= pointB[0].y &&
            converted.y <= pointB[2].y ) return YES;
    }
    return NO;
}
-(BOOL)validInset{
//获取(外部限制)的顶点
CGRect outerLimit=self.superview.bounds;
CGPoint pointA[]={
CGPointMake(outerLimit.origin.x,outerLimit.origin.y),
CGPointMake(outerLimit.size.width,outerLimit.origin.y),
CGPointMake(outerLimit.size.width、outerLimit.size.height),
CGPointMake(outerLimit.origin.x,outerLimit.size.height)};
//调整外部限制的边界
点A[0]。x+=插入,点A[0]。y+=插入;
点A[1]。x-=插入,点A[1]。y+=插入;
点A[2]。x-=插入,点A[2]。y-=插入;
点A[3]。x+=插入,点A[3]。y-=插入;
//获取B(内部视图)的顶点
CGRect innerView=self.bounds;
CGPoint pointB[]={
CGPointMake(innerView.origin.x,innerView.origin.y),
CGPointMake(innerView.size.width,innerView.origin.y),
CGPointMake(innerView.size.width、innerView.size.height),
CGPointMake(innerView.origin.x,innerView.size.height)};
//测试B的中心是否在A内,反之亦然
CGPoint中心,已转换;
center=CGPointMake(pointB[0].x+(pointB[1].x-pointB[0].x)/2,pointB[0].y+(pointB[2].y-pointB[0].y)/2;
如果(已转换=[self convertPoint:center-toView:self.superview],
已转换.x>=pointA[0].x&&
已转换。x=pointA[0]。y&&
已转换.y=pointA[0].x&&
已转换。x=pointA[0]。y&&
已转换.y=pointA[0].x&&
已转换。x=pointA[0]。y&&
已转换.y=pointB[0].x&&
已转换.x=pointB[0].y&&
转换为.y w2(或h2

w1)
  • 当2*w2

    w2时(或2*w1

    w1)

  • 其中:

    • r=视图
      ui
      中心与顶点之间的距离
    • w=视图最短边的长度
      ui
    • h=视图最长边的长度
      ui
    如果您面对其中一种情况,您应该使用另一种方法,如中的方法

    -(BOOL) validInset {
        // Get the vertices of A (outerLimit)
        CGRect outerLimit = self.superview.bounds;
        CGPoint pointA[] = {
            CGPointMake(outerLimit.origin.x,   outerLimit.origin.y    ),
            CGPointMake(outerLimit.size.width, outerLimit.origin.y    ),
            CGPointMake(outerLimit.size.width, outerLimit.size.height ),
            CGPointMake(outerLimit.origin.x,   outerLimit.size.height )};
        // Adjust outerLimit's borders
        pointA[0].x += INSET, pointA[0].y += INSET;
        pointA[1].x -= INSET, pointA[1].y += INSET;
        pointA[2].x -= INSET, pointA[2].y -= INSET;
        pointA[3].x += INSET, pointA[3].y -= INSET;
        // Get the vertices of B (innerView)
        CGRect innerView = self.bounds;
        CGPoint pointB[] = {
            CGPointMake(innerView.origin.x,   innerView.origin.y    ),
            CGPointMake(innerView.size.width, innerView.origin.y    ),
            CGPointMake(innerView.size.width, innerView.size.height ),
            CGPointMake(innerView.origin.x,   innerView.size.height )};
        // Test if the center of B is inside A or vice versa
        CGPoint center, converted;
        center = CGPointMake(pointB[0].x + (pointB[1].x - pointB[0].x)/2, pointB[0].y + (pointB[2].y - pointB[0].y)/2);
        if( converted = [self convertPoint:center toView:self.superview],
            converted.x >= pointA[0].x && 
            converted.x <= pointA[1].x &&
            converted.y >= pointA[0].y &&
            converted.y <= pointA[2].y ) return YES;
        center = CGPointMake(pointA[0].x + (pointA[1].x - pointA[0].x)/2, pointA[0].y + (pointA[2].y - pointA[0].y)/2);
        if( converted = [self convertPoint:center toView:self.superview],
            converted.x >= pointA[0].x && 
            converted.x <= pointA[1].x &&
            converted.y >= pointA[0].y &&
            converted.y <= pointA[2].y ) return YES;
        // Test if vertices of B are inside A or vice versa
        for (int i = 0; i < 4; i++) {
            if( converted = [self convertPoint:pointB[i] toView:self.superview],
                converted.x >= pointA[0].x && 
                converted.x <= pointA[1].x &&
                converted.y >= pointA[0].y &&
                converted.y <= pointA[2].y ) return YES;
            if( converted = [self.superview convertPoint:pointA[i] toView:self],
                converted.x >= pointB[0].x && 
                converted.x <= pointB[1].x &&
                converted.y >= pointB[0].y &&
                converted.y <= pointB[2].y ) return YES;
        }
        return NO;
    }