//
// ViewController.swift
// DemoApp
//
// Created by liuan on 2020/4/23.
// Copyright © 2020 anguo.com. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var animationView:UIView!
override func viewDidLoad()
{
super.viewDidLoad()
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (timer:Timer) in
print("Timer action...")
//在动画中的使用
self.animationView=UIView(frame: CGRect(x: 0, y: 40, width: 20, height: 50))
self.animationView.backgroundColor = .orange
self.view.addSubview(self.animationView)
UIView.animate(withDuration: 1.0, animations: {
self.animationView.frame=CGRect(x: 200, y: 40, width: 50, height: 50)
}){(end:Bool) in
print("Animation done.")
}
}
//在线程中
Thread.detachNewThread {
print("分离一个新的线程,并在新的线程中执行闭包语句中的任务")
}
//在调度对立中。异步执行闭包语句中的任务
DispatchQueue.main.async{
print("DispatchQueue.main.async")
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2.0) {
print("等待两s后,异步执行闭包语句中的任务,")
}
}
}
031_swift_闭包的实际应用
猜你喜欢
转载自blog.csdn.net/mp624183768/article/details/105890731
今日推荐
周排行