如何在imageView SWIFT中更改UIImage的位置 [英] how do i alter the position of a UIImage inside a imageView SWIFT

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

问题描述

我有一个 UIImage 名为 image 我想在 imageView 所以它可以稍微拖动,大约30px。有人能告诉我怎么做吗?这是我所要做的,但它不是正确的。

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(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选择身份检查器。
在用户定义的运行时属性部分中,点击左侧的加号。将密钥路径编辑为contentSize(除大小中的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)

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

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