如何更改 UIImageView 中 UIImage 的位置 [英] How do I alter the position of a UIImage inside a UIImageView

查看:47
本文介绍了如何更改 UIImageView 中 UIImage 的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 imageUIImage 我想改变它在 imageView 内的位置,以便可以稍微向下拖动它30像素.有人可以告诉我该怎么做吗?这是我要做的,但结果不正确.

I have a UIImage called image I want to change the position of it inside the imageView so it can be dragged down slightly, roughly 30px. Can some one tell me how to do it? This is where I am up to but its not coming out correct.

var image = UIImage()
var imageView = UIImageView(frame: CGRectMake(0, 0, view.frame.size.height * 0.22, view.frame.size.height * 0.22))
        imageView.center = CGPointMake(self.view.center.x, view.frame.size.height * 0.414)
        imageView.image = self.image
            imageView.layer.cornerRadius = imageView.frame.size.width / 2
            imageView.layer.borderWidth = 2.0
            imageView.layer.borderColor = UIColor(red: 254.0/255, green: 216.0/255, blue: 0/255, alpha: 1.0).CGColor
            imageView.clipsToBounds = true
            imageView.layer.contentsRect = CGRectMake(0, 20, imageView.frame.size.width, imageView.frame.size.height) //This is where I have being trying to do it but no success.
        imageView.contentMode = .ScaleAspectFill
        view.addSubview(imageView)

推荐答案

简答:你没有.

您要做的是将图像视图添加为另一个视图的子视图.做到这一点的简单方法是将图像视图放在滚动视图中,受到限制,以便它可以滚动的唯一位置是向下,并且只能稍微向下滚动.然后就可以实现零代码滚动了.

What you would do is add the image view as a subview of another view. The trivial way to do this would be to put the image view in a scroll view, constrained so that the only place it can scroll is down, and only slightly. Then you could achieve the scrolling with zero code.

这并不是一个真正的编码问题——它更像是一个 Interface Builder 问题.您需要设置滚动视图.滚动视图是一种可让您查看较大视图的一部分的视图.

This isn't really a coding problem - it's more of an Interface Builder problem. You need to set up a scroll view. A scroll view is a view that lets you look at a portion of a larger view.

您可以将滚动视图想象为一张纸,其中有一个矩形孔.您将一张更大的纸放在它下面(滚动视图的内容视图),然后您可以滑动更大的纸并通过孔查看它的不同部分.

You can think of a scroll view like a piece of paper with a rectangular hole in it. You put a bigger piece of paper under it (The scroll view's content view) and you can slide the bigger piece of paper around and view different parts of it through the hole.

下面是你的设置方法.

将滚动视图拖到您的视图控制器上.调整大小并为其添加约束以将其放置在您想要的位置.例如,如果您希望图像视图的大小为 300x300 点,并且希望能够向上或向下拖动 20 点,则将滚动视图提高 20 点.(宽:300,高:320)

Drag a scroll view onto your view controller. Size it and add constraints to it to position it where you want. If you want your image view to be 300x300 points in size, for example, and want to be able to drag it up or down by 20 points, then make the scroll view 20 points taller. (w: 300, h: 320)

选择滚动视图内的视图并将其宽度设置为与滚动视图相同的宽度,但比滚动视图高 20 点.(宽:300,高:340).添加约束以锁定它的高度和宽度.

Select the view inside the scrollview and set it's width to the same width as it's scrollview, but 20 points taller than the scroll view. (w: 300, h: 340). Add constraints to lock it's height and width.

现在您有了一个足够大的滚动视图,可以容纳 300x300 点的图像,顶部和底部总共有 20 个空白点.

Now you have a scroll view that's big enough for a 300x300 point image, with 20 points of total white space at the top and bottom.

您创建了一个比它大 20 磅的内容视图,因此它可以在滚动视图中上下滑动 20 磅.

You've created a content view that's 20 points bigger than that, so it can slide up or down by 20 points in the scroll view.

将您的 300x300 点图像视图拖到滚动视图内的视图中,为其分配图像,并添加约束以锁定其大小并在其父视图中水平和垂直居中.

Drag your 300x300 point image view into the view inside the scroll view, assign an image to it, and add constraints to lock it's size and center it horizontally and vertically in it's superview.

最后一步是设置滚动视图的内容大小.通常,您只需将滚动视图的内容大小设置为其内容视图的大小.您可以通过将这段代码添加到视图控制器的 viewDidLoad 来做到这一点:

The final step is to set the content size of the scroll view. Normally you just set a scroll view's content size to the size of it's content view. You can do that by adding this bit of code to your view controller's viewDidLoad:

(假设您已将一个名为 theScrollView 的插座连接到您的滚动视图)

(Assuming you've connected an outlet to your scrollview that's called theScrollView)

//Get the first (and only) subview of the scrollView.
let subview = theScrollView.subviews[0] as! UIView;

//Make the scroll view's contentSize the same size as the content view.
theScrollView!.contentSize = subview.bounds.size;

也可以在没有任何代码的情况下设置滚动视图的内容大小.您将使用称为用户定义的运行时属性"的 IB(Interface Builder)功能.以下是您的做法:(如果您使用这种方法,请不要将上面的代码添加到 viewDidLoad)

It's also possible to set the content size of the scroll view without any code at all. You'd use a feature of IB (Interface Builder) called "User Defined Runtime Attributes". Here's how you'd do that: (If you use this approach don't add the code above to viewDidLoad)

在 IB 中选择滚动视图.按命令选项 3 选择身份检查器".在标题为用户定义的运行时属性"的部分中,点击左侧的加号.将 Key Path 编辑为contentSize";(除Size"中的S"外,所有小写字母.这非常重要.)按 Enter 键更改键路径.然后点击类型"列并选择大小".值字段将显示{0,0}".输入您想要的内容大小:(在上面的示例中为{300,340}".)

Select the scroll view in IB. Press command-option 3 to select the "identity inspector". In the section headed "User Defined Runtime Attributes", tap the plus sign on the left. Edit the Key Path to "contentSize" (all lower case except the "S" in "Size". That's very important.) Press enter to change the key path. Then tap on the "type" column and select "size". The value field will show "{0,0}". Enter your desired content size: ("{300,340}" in the example above.)

它的作用是告诉 IB 在运行时,查找名为contentSize"的属性.在选定对象(滚动视图)中.将该属性设置为 CGSize 类型的指定值.

What this does is tell IB "At runtime, look for a property called "contentSize" in the selected object (the scroll view.) Set that property to the specified value of type CGSize.

一旦你完成了你的 IB 身份检查员"应该是这样的:

Once you're done your IB "identity inspector" should look like this:

请注意,如果您在使用用户定义的运行时属性"时获得了错误的键名然后当您显示该视图控制器时,应用程序会崩溃,并显示一条非常神秘的消息.

Note that if you get a key name wrong when using "User Defined Runtime Attributes" then the app crashes when you display that view controller, with a very cryptic message.

默认情况下,滚动视图让您过冲".拖动它们的内容时,然后在松开时弹回原位.您可以通过取消选中反弹"来关闭该功能.IB属性检查器"中的复选框(命令选项 4)

By default scrollviews let you "overshoot" when dragging their contents around, and then bounce back into place when you let go. You can turn that feature off by unchecking the "Bounce" checkbox in the IB "Attributes Inspector" (command option 4)

这篇关于如何更改 UIImageView 中 UIImage 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆