【问题标题】:Align an image horizontally and vertically in swift快速水平和垂直对齐图像
【发布时间】:2016-01-02 01:19:06
【问题描述】:

我想将另一个图像(水平和垂直)对齐以创建动画。动画很简单:心脏会在 Angry Bedou 的标志内“振动”,但他必须在该图像中“居中”。

我正在尝试以下方法:

    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")
        }

但是当我构建应用程序时它不会集中。动画有效,但对齐无效。我希望在纵向模式下在每个设备中进行对齐工作。

我看了故事板,图像很好。我做错了什么?

【问题讨论】:

  • 通过情节提要?我做到了。
  • 我不知道该怎么做=/
  • 很抱歉@LeoDabus,但我不明白您给出的解决方案
  • 我知道如何制作动画。我不知道该怎么做,就是把心放在图像“愤怒的贝都”的中心,每个设备的作品。事实上,它在任何设备测试中都不起作用。
  • 首先分解你的问题,如何将它定位在其他图像中心

标签: ios swift cabasicanimation


【解决方案1】:

试试这个:

这将使您的 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))

【讨论】:

    【解决方案2】:

    您必须使用屏幕 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
    }
    

    如果您的图像与设备屏幕的尺寸不同,您必须使用较大的图像框架 midX 减去较小的图像框架 midX 并对高度执行相同操作:

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

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

    【讨论】:

    • 它没有用。心脏图像不在愤怒的贝都的图像中。 =/
    • @ZéMoreira 我猜不出你做错了什么,你需要展示你的代码。现在忘记动画了
    • 我在本主题中首先发布的代码中做错了什么?我认为这是我能得到的最佳答案
    • 这不是 SO 的工作方式。我不会做你的全部作业。一次问一个问题。您甚至不知道如何将您的图片放在中心位置,并且您已经在询问如何为其设置动画
    • 我不是在问如何制作动画,我已经做到了。在代码中你可以看到它。我的疑问是:为什么心脏图像不与愤怒的图像对齐。我想知道我在代码中做错了什么,因为对我来说,没关系。我把心脏的位置做成:“heartImg.frame = CGRect(x:angerousBedousImg.frame.size.width / 2, y:angerousBedousImg.frame.size.height / 2, width: heartImg.frame.size.width, height: heartImg.frame.size.height) " 但我不知道为什么,它不对齐。它总是在图像的中心上方一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 2012-05-17
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多