繁体   English   中英

当旋转设备和框架的设置与纵向设置不同时,视图会更改坐标

[英]View changes coordinates when rotating device and frame is set differently than in portrait

所以,我有这种情况:

  • ImageView在情节提要上具有围绕视图的所有4个约束
  • OverlayView在其顶部,但添加在self.view上,而不是在imageview上(浅绿色背景)
  • OverlayView用于在imageview顶部绘制事物
  • 除一件事外,其他所有东西都运行良好:当我旋转设备时,我从叠加视图中看到的图形丢失了。 它的框架设置正确,我认为它的尺寸正确,但是在较小的叠加视图上边界设置不正确。

旋转前的图片:

旋转前的图像

旋转后的图像:

在此处输入图片说明

因此,如果您理解我的意思,我需要某种方式将坐标系从人像缩放/移动到风景之一。

这是我的测试viewcontroller的样子:

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) SHKDrawingView *overlayView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.blueColor;
    self.imageView.image = [UIImage imageNamed:@"asset"];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.userInteractionEnabled = true;
    _overlayView = [[SHKDrawingView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_overlayView];
    _overlayView.clipsToBounds = true;
    _overlayView.backgroundColor = [UIColor.greenColor colorWithAlphaComponent:0.5];
    _overlayView.frame = AVMakeRectWithAspectRatioInsideRect(self.imageView.image.size, self.imageView.frame);
    _overlayView.center = _imageView.center;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _overlayView.frame = AVMakeRectWithAspectRatioInsideRect(self.imageView.image.size, self.imageView.frame);
    _overlayView.center = _imageView.center;
    NSLog(@"%@", NSStringFromCGRect(_overlayView.frame));
}


@end

这就是我的画图:


CGFloat lineWidth = 5;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.line = [[NSMutableArray alloc] init];
        self.sublayers = [[NSMutableArray alloc] init];

    }
    return self;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self flattenImage];
}



-(void)flattenImage {
    [self updateFlattenedLayer];
    [_line removeAllObjects];
}

-(CGRect) calculateRectBetweenLastPoint:(CGPoint)lastPoint newPoint:(CGPoint)newPoint {

    CGFloat originX = MIN(lastPoint.x, newPoint.x) - (lineWidth / 2);
    CGFloat originY = MIN(lastPoint.y, newPoint.y) - (lineWidth / 2);

    CGFloat maxX = MAX(lastPoint.x, newPoint.x) + (lineWidth / 2);
    CGFloat maxY = MAX(lastPoint.y, newPoint.y) + (lineWidth / 2);

    CGFloat width = maxX - originX;
    CGFloat height = maxY - originY;

    return CGRectMake(originX, originY, width, height);
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.allObjects.firstObject;
    if (touch != nil) {
        CGPoint newTouchPoint = [touch locationInView:self];
        [self.line addObject:[NSValue valueWithCGPoint:newTouchPoint]];

        CGPoint lastTouchPoint = [[self.line lastObject] CGPointValue];

        CGRect rect = [self calculateRectBetweenLastPoint:lastTouchPoint newPoint:newTouchPoint];
        [self.layer setNeedsDisplayInRect:rect];
    } else {
        return;
    }
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
    CAShapeLayer *drawingLayer = CAShapeLayer.new;
    if (_drawingLayer != nil) {
        drawingLayer = _drawingLayer;
    }

    UIBezierPath *linePath = UIBezierPath.new;

    drawingLayer.contentsScale = UIScreen.mainScreen.scale;

    for (int index = 0; index < self.line.count; index++) {
        CGPoint point = [self.line[index] CGPointValue];
        if (index == 0) {
            [linePath moveToPoint:point];
        } else {
            [linePath addLineToPoint:point];
        }
    }

    drawingLayer.path = linePath.CGPath;
    drawingLayer.opacity = 1;
    drawingLayer.lineWidth = lineWidth;
    drawingLayer.lineCap = kCALineCapRound;
    drawingLayer.fillColor = UIColor.clearColor.CGColor;
    drawingLayer.strokeColor = UIColor.redColor.CGColor;

//    if (_drawingLayer != nil) {
        _drawingLayer = drawingLayer;
        [self.layer addSublayer:drawingLayer];
//    }
}

-(void)updateFlattenedLayer {
    NSError *error = nil;
    if (self.drawingLayer != nil) {
        if (@available(iOS 11.0, *)) {
            CAShapeLayer *optionalDrawing = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self.drawingLayer requiringSecureCoding:NO error:&error]];
            if (optionalDrawing != nil) {
                [self.layer addSublayer: optionalDrawing];
            }
        }
    }
}

-(void)checkIfTooManyPoints {
    CGFloat maxPoints = 25;
    if (self.line.count > maxPoints) {
        [self updateFlattenedLayer];
        // we leave two points to ensure no gaps or sharp angles
        [self.line removeObjectsInRange:NSMakeRange(0, maxPoints-2)];
    }
}

-(void)clear {
    [self emptyFlattenedLayers];
    [self.drawingLayer removeFromSuperlayer];
    self.drawingLayer = nil;
    [self.line removeAllObjects];
    [self.layer setNeedsDisplay];
}

-(void)emptyFlattenedLayers {
    for (CALayer *layer in self.sublayers) {
        if ([layer isKindOfClass:[CAShapeLayer class]]) {
            [layer removeFromSuperlayer];
        }
    }
}

@end```

Any help would be appreciated. Thank you very much

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM