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

查看:503
本文介绍了如何更改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点. (w:300,h: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点. (w:300,h: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(接口构建器)的一项功能,即用户定义的运行时属性".操作方法如下:(如果使用这种方法,请不要将上面的代码添加到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". (除大小"中的"S"外,所有小写字母都非常重要.)这很重要.)按Enter更改键路径.然后点击"type"按钮.列,然后选择大小".值字段将显示"{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.

默认情况下,滚动视图可让您超调"拖动它们的内容时,然后放开时弹回原位.您可以通过取消选中跳出"(Bounce)来关闭该功能. 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天全站免登陆