ios - 如何克隆 UIImageView

标签 ios xcode uiimageview

我有一个 UIImageView,我想复制它并将它放在屏幕上的某个位置。我该怎么做?

我目前只知道如何手动复制和粘贴图像并为每个图像制作一个单独的 IBOutlet,但这样做效率很低,因为我想制作一个永远产生障碍物(UIImageViews)的游戏所以我做不到手动方式。

最佳答案

您要确保也匹配所有属性,例如大小、剪裁、图像纵横比、不透明度等。

CGPoint locationOfCloneImageView = CGPointMake(0, 0);//x and y coordinates of where you want your image. (More specifically, the x and y coordinated of where you want the CENTER of your image to be)

UIImageView *cloneImageView = [[UIImageView alloc] initWithImage:originalImageView.image];
cloneImageView.frame = CGRectMake(0, 0, originalImageView.frame.size.width, originalImageView.frame.size.height);//same size as old image view
cloneImageView.alpha = originalImageView.alpha;//same view opacity
cloneImageView.layer.opacity = originalImageView.layer.opacity;//same layer opacity
cloneImageView.clipsToBounds = originalImageView.clipsToBounds;//same clipping settings
cloneImageView.backgroundColor = originalImageView.backgroundColor;//same BG color
cloneImageView.tintColor = originalImageView.tintColor;//matches tint color.
cloneImageView.contentMode = originalImageView.contentMode;//matches up things like aspectFill and stuff.
cloneImageView.highlighted = originalImageView.highlighted;//matches whether it's highlighted or not
cloneImageView.opaque = originalImageView.opaque;//matches can-be-opaque BOOL
cloneImageView.userInteractionEnabled = originalImageView.userInteractionEnabled;//touches are detected or not
cloneImageView.multipleTouchEnabled = originalImageView.multipleTouchEnabled;//multi-touches are detected or not
cloneImageView.autoresizesSubviews = originalImageView.autoresizesSubviews;//matches whether or not subviews resize upon bounds change of image view.
//cloneImageView.hidden = originalImageView.hidden;//commented out because you probably never need this one haha... But if the first one is hidden, so is this clone (if uncommented)
cloneImageView.layer.zPosition = originalImageView.layer.zPosition+1;//places it above other views in the parent view and above the original image. You can also just use `insertSubview: aboveSubview:` in code below to achieve this.
[originalImageView.superview addSubview:cloneImageView];//adds this image view to the same parent view that the other image view is in.

cloneImageView.center = locationOfCloneImageView;//set at start of code.

关于ios - 如何克隆 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25439234/

相关文章:

ios - 'NSInvalidArgumentException',原因 : 'Receiver has no segue with identifier ' unwind''

ios - 如何将目标添加到 UIImageView 的封闭初始化中

ios - .removeFromSuperview 不适用于 UIImageView

ios - 在 ScrollView 中的 UIImage 中检测触摸位置

javascript - React-Native 自定义对话框

android - 使用 Phongap 的不同移动设备的背景图像大小

ios - 与 UIButton 一起动态发送字符串?

ios - 如何调整 UIButton 的 imageSize?

viewDidLoad 中的 iOS 7 setContentOffset 重置为 0

ios - 如何获得iOS应用程序的root特权?