【问题标题】:commitAnimations is not resizing my UIImageView subviewcommitAnimations 没有调整我的 UIImageView 子视图的大小
【发布时间】:2010-05-19 01:51:23
【问题描述】:

我创建了一个 UIView 作为子视图,并在该子视图中添加了一个 UIImageView 作为子视图。

UIView *viewCreated;
UIButton *buttonCreated;
UIImageView *imageViewCreated;
CGRect myFrame = CGRectMake(0, 0, 1024, 1024);
viewCreated = [[UIView alloc] initWithFrame:myFrame];
[viewCreated setTag:intTag];
viewCreated.backgroundColor = [UIColor redColor];
[self.view addSubview:viewCreated];
[self randomize];
UIImage *d1Image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d.png", randomNumber]];
imageViewCreated = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f, 1024.0f)];
[imageViewCreated setImage:[UIImage imageNamed:[NSString stringWithFormat:@"image%d.png", randomNumber]]]; 
[viewCreated addSubview:imageViewCreated];
//[imageViewCreated release];
return [viewCreated autorelease];

但是当这段代码执行时,只有第一个子视图被动画和调整大小。 UiimageView 向左移动 200 像素,但没有调整大小。

NSLog(@"sender tag %i",[sender tag]);   
UIView *currentView = [self.view viewWithTag:[sender tag]];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.5];  
currentView.frame = CGRectMake(-200, 0,40, 102);
[UIView commitAnimations];

我很确定我没有以编程方式正确创建子视图,因为当我在 Interface Builder 中执行此操作时,它会按预期工作。

我是否必须做一些特定的事情才能将第二个子视图的行为实际附加到第一个子视图?

【问题讨论】:

  • 我可能错过了一些东西,但 1024 x 1024 在 iPhone 上似乎是一个非常大的 UIView。
  • 你是对的。我贴错标签了。应该是 iPad。感谢您指出这一点。

标签: objective-c xcode ipad interface-builder


【解决方案1】:

一些建议:

  • 使用 UIImageView 的 contentMode 属性。默认模式为UIViewContentModeScaleToFillUIViewContentModeScaleAspectFit 之类的东西可能会更好。

  • 确保在您的代码中某处,您不会不小心将 autoresizesSubviews 属性设置为 NO。

  • 考虑使用transform 属性和CGAffineTransformMakeScale 函数,而不是使用框架大小来缩放视图。要使其移动,您还可以使用CGAffineTransformMakeTranslation

【讨论】:

  • 感谢您的回复。它让我走上了正轨。 imageViewCreated.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
【解决方案2】:

我最终使用它来解决我的问题。完美运行。

imageViewCreated.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2013-07-20
    • 2014-09-02
    • 2013-04-12
    相关资源
    最近更新 更多