那就写个就是了
调用视图的仿射变换函数就可以了吧
view.layer.setAffineTransform( CGAffineTransform(translationX: 2, y: 0))
这就可以让视图往左变换了,往右就是-2咯,改Y就是上下呗
具体代码我就粘在下面咯,反正也没多少行
//
// UIView+ZHYShake.swift
// UIViewShake
//
// Created by ZHY on 2017/8/16.
// Copyright © 2017年 ZHY. All rights reserved.
//
import UIKit
/// 抖动方向
///
/// - horizontal: 水平抖动
/// - vertical: 垂直抖动
public enum ZHYShakeDirection: Int {
case horizontal
case vertical
}
extension UIView {
/// ZHY 扩展UIView增加抖动方法
///
/// - Parameters:
/// - direction: 抖动方向 默认水平方向
/// - times: 抖动次数 默认5次
/// - interval: 每次抖动时间 默认0.1秒
/// - offset: 抖动的偏移量 默认2个点
/// - completion: 抖动结束回调
public func shake(direction: ZHYShakeDirection = .horizontal, times: Int = 5, interval: TimeInterval = 0.1, offset: CGFloat = 2, completion: (() -> Void)? = nil) {
//移动视图动画(一次)
UIView.animate(withDuration: interval, animations: {
switch direction {
case .horizontal:
self.layer.setAffineTransform(CGAffineTransform(translationX: offset, y: 0))
case .vertical:
self.layer.setAffineTransform(CGAffineTransform(translationX: 0, y: offset))
}
}) { (complete) in
//如果当前是最后一次抖动,则将位置还原,并调用完成回调函数
if (times == 0) {
UIView.animate(withDuration: interval, animations: {
self.layer.setAffineTransform(CGAffineTransform.identity)
}, completion: { (complete) in
completion?()
})
}
//如果当前不是最后一次,则继续动画,偏移位置相反
else {
self.shake(direction: direction, times: times - 1, interval: interval, offset: -offset, completion: completion)
}
}
}
}
//一个简单的调用,全部使用默认值,抖动完成后不做任何操作
view.shake()
//这样就设置为垂直抖动啦。。。
view.shake(direction: .vertical)
label.shake(direction: .horizontal, times: 9, interval: 0.03, offset: 3) {
print("我抖完了,该你了。")
}