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
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.") |
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
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)") | |
} |
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
// 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. |
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
// 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. |
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
// 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. |
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
// 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! |
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
// 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) | |
} |
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
@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 ... | |
} |
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
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) | |
} |
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
@IBOutlet var easeInOutRepeatLabel: UILabel! | |
easeInOutRepeatLabel.layer.removeAllAnimations() |
OlderNewer