Swift-在3个方向(顶部,右侧或底部)随机旋转UIImageView

时间:2018-08-10 07:46:07

标签: ios swift xcode

我是Swift的新手,确实需要一些帮助。 我有一个带有箭头图像的UIImageView。我希望箭头在按钮上的方向随机改变,仅在三个方向上单击:顶部,右侧和底部。 我发现可以使用UIImageView来旋转CGAffineTransform,但是我该怎么办?

  1. 从旋转中排除左方向吗?
  2. 使箭头随机更改其方向(上,右和下) ?

here's a screen I work with

2 个答案:

答案 0 :(得分:0)

产生一个随机数字:

@Entity
@NamedQuery(name = "Person.findByPhone",
        query = "select p from Person as p join p.phoneNumbers as pn where pn.number = :number")
@Table(uniqueConstraints={
        @UniqueConstraint(columnNames = {"lastName", "firstName", "patronymic"})
})
public class Person {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String lastName;

    @NotNull
    private String firstName;

    private String patronymic;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "person_phone_numbers", joinColumns = @JoinColumn(name = "person_id"))
    private Set<PhoneNumber> phoneNumbers = new HashSet<>();

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "person_addresses", joinColumns = @JoinColumn(name = "person_id"))
    @AttributeOverrides({
            @AttributeOverride(name = "addressLine1", column = @Column(name = "house_number")),
            @AttributeOverride(name = "addressLine2", column = @Column(name = "street"))
    })
    private Set<Address> addresses = new HashSet<>();

    public Person() {
    }

    public Person(String lastName, String firstName, String patronymic,
                  Set<PhoneNumber> phoneNumbers, Set<Address> addresses) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.patronymic = patronymic;
        this.phoneNumbers = phoneNumbers;
        this.addresses = addresses;

    }
getters and setters
}

答案 1 :(得分:0)

希望这对您有帮助:

    let degree = [Double.pi / 2, Double.pi, Double.pi * 2]
    let randomIndex = Int(arc4random_uniform(UInt32(degree.count)))
    UIView.animate(withDuration: 1.0) {
        self.imageView.transform = CGAffineTransform(rotationAngle: CGFloat(degree[randomIndex]))
    }

enter image description here