UIActivityIndicator 사용하기
아래 이미지와 소스코드를 비교해보시면 좀 더 이해하기 편합니다.
궁금하신점은 댓글로 달아주세요.
해피코딩 :)
Preview
Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ActivityIndicatorVC.swift | |
// UIKit component handling | |
// | |
// Created by Taehyeon Han on 2018. 8. 9.. | |
// Copyright © 2018년 calmone. All rights reserved. | |
// | |
import UIKit | |
class ActivityIndicatorVC: BaseViewController { | |
lazy var activityIndicator: UIActivityIndicatorView = { | |
// Create an indicator. | |
let activityIndicator = UIActivityIndicatorView() | |
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50) | |
activityIndicator.center = self.view.center | |
// Also show the indicator even when the animation is stopped. | |
activityIndicator.hidesWhenStopped = false | |
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white | |
// Start animation. | |
activityIndicator.startAnimating() | |
return activityIndicator | |
}() | |
lazy var button: UIButton = { | |
// Create a button. | |
let button = UIButton(frame: CGRect(x:0, y:0, width:60, height:60)) | |
button.backgroundColor = UIColor.red | |
button.layer.masksToBounds = true | |
button.layer.cornerRadius = 30.0 | |
button.setTitle("Stop", for: .normal) | |
button.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height-50) | |
button.addTarget(self, action: #selector(onClickButton(sender:)), for: .touchUpInside) | |
return button | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
// Set the background color to black. | |
self.view.backgroundColor = UIColor.black | |
// Add UIActivityIndicatorView on view | |
self.view.addSubview(self.activityIndicator) | |
// Add UIButton on view | |
self.view.addSubview(self.button) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// Button event. | |
@objc func onClickButton(sender: UIButton){ | |
if self.activityIndicator.isAnimating { | |
self.activityIndicator.stopAnimating() | |
self.button.setTitle("Start", for: .normal) | |
self.button.backgroundColor = UIColor.blue | |
} | |
else { | |
self.activityIndicator.startAnimating() | |
self.button.setTitle("Stop", for: .normal) | |
self.button.backgroundColor = UIColor.red | |
} | |
} | |
} |
Github
https://github.com/calmone/iOS-UIKit-component
Reference
- UIActivityIndicatorView
https://developer.apple.com/reference/uikit/uiactivityindicatorview
'iOS > Swift' 카테고리의 다른 글
[iOS UIKit in Swift 4] UIWindow 사용하기 (0) | 2019.03.11 |
---|---|
[iOS UIKit in Swift 4] UISearchBar 사용하기 (0) | 2019.03.07 |
[iOS UIKit in Swift 4] UIDatePicker 사용하기 (0) | 2019.03.05 |
[iOS UIKit in Swift 4] UISwitch 사용하기 (0) | 2019.03.04 |
[iOS UIKit in Swift 4] UISlider 사용하기 (0) | 2019.03.04 |