简体   繁体   English

快速水平和垂直对齐图像

[英]Align an image horizontally and vertically in swift

在此处输入图像描述

I want to align an image in another (horizontally and vertically) to create an animation.我想对齐另一个图像(水平和垂直)以创建动画。 The animation is simple: the heart will "vibrate" inside the Angry Bedou's logo, but he must be "centered" in that image.动画很简单:心脏会在 Angry Bedou 的徽标内“振动”,但他必须在那个图像中“居中”。

I was trying the following:我正在尝试以下操作:

    func animate() {
    heartImg.frame = CGRectMake(angryBedousImg.frame.size.width / 2, angryBedousImg.frame.size.height / 2, heartImg.frame.size.width, heartImg.frame.size.height)

    let heartAnimation = CABasicAnimation(keyPath: "position")
    heartAnimation.duration = 0.09
    heartAnimation.repeatCount = .infinity
    heartAnimation.autoreverses = true
    heartAnimation.fromValue = NSValue(CGPoint: CGPointMake(heartImg.center.x - 1, heartImg.center.y))
    heartAnimation.toValue = NSValue(CGPoint: CGPointMake(heartImg.center.x + 1, heartImg.center.y))
    heartImg.layer.addAnimation(heartAnimation, forKey: "position")
        }

But it wont centralizes when i build the app.但是当我构建应用程序时它不会集中。 The animation works, but not the alignment.动画有效,但对齐无效。 I want the alignment work in every device in portrait mode.我希望在纵向模式下的每台设备上都能进行对齐。

在此处输入图像描述

I look at the storyboard, and the images are fine.我看了故事板,图像很好。 What i'm doing wrong?我做错了什么?

Try this:试试这个:

This will make your heartImage centre to the background Image.这将使您的 heartImage 居中于背景图像。

    let yourImage = UIImage(named: "YourImageName")
    let heartImg = UIImageView()
    heartImg.image = yourImage
    heartImg.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(heartImg)
    self.view.addConstraint(NSLayoutConstraint(item: heartImg, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.yourLogoImage, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: heartImg, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.yourLogoImage, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0))
    self.view.addConstraint(NSLayoutConstraint(item: heartImg, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 40))
    self.view.addConstraint(NSLayoutConstraint(item: heartImg, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1.0, constant: 40))

You have to use your screen midX and midY properties and subtract the image's half width and half height:您必须使用屏幕 midX 和 midY 属性并减去图像的半宽和半高:

if let imageURL = NSURL(string:"http://i.stack.imgur.com/Xs4RX.jpg"), data = NSData(contentsOfURL: imageURL), image = UIImage(data: data), image2URL = NSURL(string:"https://graph.facebook.com/10203430834250391/picture?type=large"), data2 = NSData(contentsOfURL: image2URL), image2 = UIImage(data: data2) {

    let imageView = UIImageView(frame: UIScreen.mainScreen().bounds)
    imageView.image = image
    imageView.contentMode = UIViewContentMode.ScaleAspectFit

    let image2View = UIImageView(frame: CGRect(x: UIScreen.mainScreen().bounds.midX-100, y: UIScreen.mainScreen().bounds.midY-100, width: 200, height: 200))
    image2View.image = image2
    image2View.contentMode = UIViewContentMode.Center
    imageView.addSubview(image2View)
    imageView
}

If your image it is not the same size of your device screen you have to use the larger image frame midX minus the smaller image frame midX and do the same for the height:如果您的图像与您的设备屏幕大小不同,您必须使用较大的图像框架 midX 减去较小的图像框架 midX 并对高度执行相同的操作:

 heartImg.frame = CGRectMake(angryBedousImg.frame.size.width / 2, angryBedousImg.frame.size.height / 2, heartImg.frame.size.width, heartImg.frame.size.height)

To

heartImg.frame = CGRectMake(angryBedousImg.frame.midX - heartImg.frame.midX, angryBedousImg.frame.midY - heartImg.frame.midY, heartImg.frame.size.width, heartImg.frame.size.height)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM