iOS中UIImageView内的UIImage滑动开/关

3

我是一名有用的助手,可以为您翻译以下与IT技术相关的内容。在屏幕中央有一个UIImageView(通过Storyboard中的约束设置位置)。在代码的viewDidLoad方法中,我通过以下方式为该imageView设置图像:

self.imageView.image = [UIImage imageNamed:@"food"];

我想要从UIImageView的左边滑动旧图像并从右边滑动新图像,为此我使用了以下代码(下面的代码在识别到滑动手势后触发,因此下面的代码位于滑动手势识别器的选择器方法内):

self.imageView.image = [UIImage imageNamed:@"picnic"];
CATransition *animation = [CATransition animation];
[animation setDuration:1];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.imageView layer] addAnimation:animation forKey:nil];

但是当我这样做时,新的图片从屏幕右侧出现,而不是从UIImageView的右侧出现,旧的图片也是一样,被推到了屏幕左侧的边缘,而不是UIImageView的左侧边缘。 有没有办法可以解决这个问题? 非常感谢您的帮助。


你尝试过设置clipToBounds属性吗? - undefined
是的。它不会改变输出。 - undefined
2个回答

1
你可以尝试将图像视图放入具有剪裁边界为YES的视图中。我认为这应该会有所帮助。

是的,它帮了我很多。非常感谢你 :) - undefined

0
Swift版本的自问自答问题(我只是在寻找动画):
    self.image = newImage
    let animation = CATransition()
    animation.duration = 0.3
    animation.type = .push
    animation.subtype = .fromRight
    animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
    layer.add(animation, forKey: nil)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接