Skip to content

Instantly share code, notes, and snippets.

View myrickchow32's full-sized avatar

Myrick Chow myrickchow32

  • RedSo
  • Hong Kong
View GitHub Profile
import UIKit
// Step 1: Import CoreNFC library
import CoreNFC
class ViewController: UIViewController {
// Step 2: Hold a reference to the NFCNDEFReaderSession object as long as the ViewController stays alive.
var nfcSession: NFCNDEFReaderSession?
@IBAction func scanNFCDemoAction(_ sender: Any) {
startNFC(alertMessage: "Please scan a NFC tag.")
extension ViewController: NFCNDEFReaderSessionDelegate {
func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
/*
Example of error message:
"Session is invalidated by user cancellation" --- User has cancelled the NFC reader dialog
"Session is invalidated due to maximum session timeout" --- Time out for scanning a NFC tag is exceeded.
"Feature not supported" --- Device does not support the NFC feature (iPhone 6S or older) or app is running at simulator
*/
print("The session was invalidated: \(error.localizedDescription)")
}
// invalidateAfterFirstRead = true
var nfcSession = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: true)
nfcSession.begin()
extension ViewController: NFCNDEFReaderSessionDelegate {
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
// Handling of [NFCNDEFMessage] and return the result String
var result = ...
// Step 8: didDetectNDEFs callback is run in background thread. All UI updates must be handled carefully.
// invalidateAfterFirstRead = false
var nfcSession = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: false)
nfcSession.begin()
extension ViewController: NFCNDEFReaderSessionDelegate {
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
// Handling of [NFCNDEFMessage] and return the result String
var result = ...
// Step 8: didDetectNDEFs callback is run in background thread. All UI updates must be handled carefully.
// invalidateAfterFirstRead = false
var nfcSession = NFCNDEFReaderSession.init(delegate: self, queue: nil, invalidateAfterFirstRead: false)
nfcSession.begin()
extension ViewController: NFCNDEFReaderSessionDelegate {
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
// Handling of [NFCNDEFMessage] and return the result String
var result = ...
// Step 8: didDetectNDEFs callback is run in background thread. All UI updates must be handled carefully.
// Definition of UILabels
@IBOutlet var easeInLabel: UILabel!
@IBOutlet var easeOutLabel: UILabel!
@IBOutlet var easeInOutLabel: UILabel!
@IBOutlet var linearLabel: UILabel!
@IBOutlet var transitionFlipFromLeftLabel: UILabel!
@IBOutlet var transitionFlipFromRightLabel: UILabel!
@IBOutlet var transitionCurlUpLabel: UILabel!
@IBOutlet var transitionCurlDownLabel: UILabel!
@IBOutlet var transitionCrossDissolveLabel: UILabel!
// All animations are done with the same length of duration --- 1.0 second
let durationOfAnimationInSecond = 1.0
func curveAnimation(view: UIView, animationOptions: UIView.AnimationOptions, isReset: Bool) {
let defaultXMovement: CGFloat = 240
UIView.animate(withDuration: durationOfAnimationInSecond, delay: 0, options: animationOptions, animations: {
view.transform = isReset ? .identity : CGAffineTransform.identity.translatedBy(x: defaultXMovement, y: 0)
}, completion: nil)
}
@IBOutlet var easeInLabelLeadingConstraint: NSLayoutConstraint!
@IBAction func startAnimationButtonAction(_ sender: Any) {
// Print out the corresponding information of easeInLabel before and after animation
print("\(isAnimated ? "easeInLabel has been moved." : "easeInLabel is at its original position.")")
print("easeInLabelLeadingConstraint: \(easeInLabelLeadingConstraint.constant)")
print("easeInLabel frame: \(easeInLabel.frame)")
print("*********************************************")
// Animation function ...
}
func curveAnimation(view: UIView, animationOptions: UIView.AnimationOptions, isReset: Bool) {
// Solution: Combine with .allowUserInteraction
let combinedAnimationOptions: UIView.AnimationOptions = [animationOptions, .allowUserInteraction]
let defaultXMovement: CGFloat = 240
UIView.animate(withDuration: durationOfAnimationInSecond, delay: 0, options: combinedAnimationOptions, animations: {
view.transform = isReset ? .identity : CGAffineTransform.identity.translatedBy(x: defaultXMovement, y: 0)
}, completion: nil)
}
@IBOutlet var easeInOutRepeatLabel: UILabel!
easeInOutRepeatLabel.layer.removeAllAnimations()