改变UIImageView的变换

时间:2018-05-02 05:31:14

标签: ios swift uiimageview transform

我尝试在我的应用中设置一个功能,当我点按视图时,该功能会更改UIImageView(他们的位置和比例)的变换。每个图像应移动到其他图像的相同位置,以使其具有滚动效果。例如,如果我有图像' a'' b'和' c',图像' a'应该转变为' b',image' b'的位置和规模。应该转变为' c'的位置和规模。最后形象' c'应该转变为' a'的位置和规模。

我设法让它按照第一次使用的方式工作。但是每次敲击都会失去位置和规模。

以下是代码:

@objc private func tapHandler(){

    for index in 0...2{
        var transform = CGAffineTransform()

        if (circlesIV[index].tag==0){
            transform = transforms[0].translatedBy(x: +100.0, y: 0.0)
            transform = transform.scaledBy(x: 0.5, y: 0.5)
        }
        else if (circlesIV[index].tag==1){
            transform = transforms[1].translatedBy(x: -200.0, y: 0.0)
        }
        else {
            transform = transforms[2].translatedBy(x: +100.0, y: 0.0)
            transform = transform.scaledBy(x: 2, y: 2)
        }
        circlesIV[index].transform = transform
    }

    var tempTag = circlesIV[0].tag
    circlesIV[0].tag = circlesIV[1].tag
    circlesIV[1].tag = circlesIV[2].tag
    circlesIV[2].tag = tempTag 
}

变换是一个包含UIImageView原始变换的数组,而circleIV是UIImageView的数组(有3个图像)。

编辑:我认为问题与用于设置图像的自动约束有关。

circlesIV[0].centerXAnchor.constraint(equalTo: topHalf.centerXAnchor).isActive = true
circlesIV[1].centerXAnchor.constraint(equalTo: topHalf.centerXAnchor, constant: +100).isActive = true
circlesIV[2].centerXAnchor.constraint(equalTo: topHalf.centerXAnchor, constant: -100).isActive = true

for index in 0...2{
    circlesIV[index].translatesAutoresizingMaskIntoConstraints = false
    circlesIV[index].centerYAnchor.constraint(equalTo: topHalf.centerYAnchor).isActive = true
    if(index==0){
        circlesIV[index].widthAnchor.constraint(equalToConstant: 150).isActive = true
        circlesIV[index].heightAnchor.constraint(equalToConstant: 150).isActive = true
    } else {
        circlesIV[index].widthAnchor.constraint(equalToConstant: 75).isActive = true
        circlesIV[index].heightAnchor.constraint(equalToConstant: 75).isActive = true
    }
}

0 个答案:

没有答案