【问题标题】:UIImageView rotation + center change cause the image to blowUIImageView 旋转 + 中心变化导致图像爆炸
【发布时间】:2017-07-10 17:33:57
【问题描述】:

我正在尝试每秒更改一次 UIImageView。

更改可以是以下两种之一: - 回转 - 位置变化

我正在使用以下内容:

这是初始化代码:

+(instancetype)newWithFrame:(CGRect)frame {
    MySateliteView *v = [[[NSBundle mainBundle] loadNibNamed:@"Satelite" owner:self options:nil] objectAtIndex:0];
    v.frame = frame;
    v.lastLocation = [SateliteCoordinate new];

    return v;
}

这是视图的完整代码:

-(void)createSateliteViewFromLocation:(SateliteLocation *)location {
     CGPoint center = [self createPointFromLocation:location];
     MySateliteView *locationView;
     for (MySateliteView *v in _satelites) {
        if (v.tag == location.sateliteNumber.integerValue)  {
           locationView = v;
           break;
        }
     }
     if (!locationView) {
        locationView = [MySateliteView newWithFrame:CGRectMake(center.x - 15, center.y - 15, 30, 30)];
        locationView rotate:location.coordinates.degree];
        locationView.tag = location.sateliteNumber.integerValue;

        [_satelites addObject:locationView];
        [UIView animateWithDuration:0.7 animations:^{
            [self addSubview:locationView];
        }];
     } else {
         if ([locationView needsNewCenter:location.coordinates]) {
         [UIView animateWithDuration:0.2 animations:^{
            locationView.bounds = CGRectMake(0, 0, 30, 30);
            locationView.center = center;
            locationView.superview.clipsToBounds = YES;
         }];
     }
     if ([locationView needsRotate:location.coordinates]) {
         [locationView rotate:location.coordinates.degree];
     }
  }
}

由于某种原因,一旦中心发生变化并应用了旋转,图像就会变大!

有人知道为什么以及我能做什么吗?

谢谢!

【问题讨论】:

  • 不应该是locationView.transform = transform;吗?
  • 我无法使用您提供的代码重现您的问题。你能提供更多细节吗?
  • 尝试在循环中运行此代码,在每个奇数情况下更改中心并在每个偶数情况下更改旋转。中心变化导致我的图像增长.. 我知道旋转本身可以改变帧大小,我无法理解的是为什么图像大小会随着我每次重新中心而改变
  • 我无法重现您的体验。旋转本身不能改变帧大小。转换使frame 返回的值无效。我的预感是自动调整大小行为或布局约束相互作用正在发生。没有可重现的例子,我无能为力。
  • 我有一个 xib 文件,上面只有一个 imageview。图像视图具有以下约束: [ 0 from top 0 from left 0 from right 0 from bottom ] 问题是,一旦我设置了一个新的中心,图像视图就会由于某种原因被炸毁。我知道这不是很多工作有,但我不确定我还能提供什么..

标签: ios objective-c uiview


【解决方案1】:

框架变得越来越大,因为您在视图中有约束,并且您手动弄乱了中心。您不应该以这种方式混合自动布局和手动布局。我建议:

  1. 删除约束并手动执行所有视图布局,方法是酌情覆盖layoutSubviewsdrawRect:,同时继续保留您已有的代码。

  2. 与约束而不是框架/中心/边界交互。您可以从 xib 中的约束创建一个 IBOutlet,以便修改它们的值。您可以在视图中覆盖系统提供的updateConstraints 方法,以确保适当地提供约束。

更新约束以移动视图的示例可能如下所示:

[UIView animateWithDuration: .7, animations: ^{
    self.constraint1.constant = 23;
    [NSLayoutConstraint deactivateConstraints: @[self.constraint2]];
    [self layoutIfNeeded];
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多