0

我想在动画中显示图像的一部分。我在 中放了一个UIImageViewUIScrollView并将 UIImageView 的框架设置为(0, 0, imageWidth, imageHeight),并将 UIScrollView 的框架宽度设置为0。这是我的代码

self.tiaoView.contentSize=CGSizeMake(316, 74);//tiaoView is an outlet of UIScrollView
[UIView animateWithDuration:5 animations:^{
    CGRect rect=self.tiaoView.frame;
    rect.size.width=316;
    self.tiaoView.frame=rect;}];

但是当我运行它时,整个图像立即显示,没有动画。

4

1 回答 1

2

UIImageView您可以通过将s设置contentMode为其中任何一个来仅显示图像的一部分(顶部、底部、左侧或右侧)

UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight

相应地设置clipsToBounds = YES和更改其框架。因为frame是动画属性,所以这种组合将允许您以动画方式仅显示图像的一部分。

例如:如果您只想显示距底部 20 个点,imageView.contentMode = UIViewContentModeBottom;请将其frames 高度设置为20UIImageView无论您设置什么框架,图像都将保持在底部边缘。

见示例代码:

UIImage *image = [UIImage imageNamed:@"myImage"];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 0, 40);
imageView.contentMode = UIViewContentModeLeft;
imageView.clipsToBounds = YES;
[self.view addSubview:imageView];

CGRect finalFrame = imageView.frame;
finalFrame.size.width = 40;

[UIView animateWithDuration:1.0 animations:^{
    imageView.frame = finalFrame;
}];

此代码通过从 0 大小 40 扩展其大小来为图像设置动画。

于 2013-06-03T08:22:49.013 回答