When uploading the profile image to firebase the app crashes getting this error "malloc: Heap corruption detected, free list is damaged at 0x280165460
* Incorrect guard value: 10751132320
"app-name"(945,0x16f5df000) malloc: * set a breakpoint in malloc_error_break to debug
I pasted the code from my entire Controller with exception to the gradient function seeing as that is not necessary.
The purpose of this controller is simply to upload an image to Firebase. IMPORTANT: This crash doesn't happen all the time but when it does it is fatal to user experience. Furthermore, this crash does not happen on my setting page which gives the user an option to add three more images to their profile. Not once has that feature crashed on me.
Please I desperately need help!
import UIKit
import Firebase
import UserNotifications
import SDWebImage
extension EnterPhotoController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let selectedImage = info[.originalImage] as? UIImage
let imageButton = (picker as? CustomImagePickerController)?.imageBttn
imageButton?.setImage(selectedImage?.withRenderingMode(.alwaysOriginal), for: .normal)
self.imageFull = true
dismiss(animated: true)
saveImageToFirebase(profImage: selectedImage ?? UIImage(named: "CrushtLogoLiam")!)
}
}
class EnterPhotoController: UIViewController {
var imageFull = Bool()
var name = String()
var birthday = String()
var age = Int()
var bio = String()
var school = String()
let selectPhotoButton: UIButton = {
let button = UIButton(type: .system)
//button.setBackgroundImage(#imageLiteral(resourceName: "top_left_profile"), for: .normal)
//button.imageView?.contentMode = .scaleAspectFit
//button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .heavy)
button.backgroundColor = .white
button.setBackgroundImage(#imageLiteral(resourceName: "CrushtLogoLiam"), for: .normal)
button.setTitleColor(.black, for: .normal)
button.heightAnchor.constraint(equalToConstant: 300).isActive = true
button.widthAnchor.constraint(equalToConstant: 140).isActive = true
button.imageView?.contentMode = .scaleAspectFill
//button.imageView?.adjustsImageSizeForAccessibilityContentSizeCategory = true
button.titleLabel?.font = UIFont.systemFont(ofSize: 20)
button.titleLabel?.adjustsFontSizeToFitWidth = true
button.addTarget(self, action: #selector(handleSelectPhoto), for: .touchUpInside)
button.layer.cornerRadius = 70
button.clipsToBounds = true
return button
}()
let label: UILabel = {
let label = UILabel()
label.textColor = .white
label.text = "Select Your Profile Picture"
label.font = UIFont.systemFont(ofSize: 30, weight: .heavy)
label.textAlignment = .center
label.adjustsFontSizeToFitWidth = true
return label
}()
let errorLabel: UILabel = {
let label = UILabel()
label.text = "Please select a photo"
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 25, weight: .heavy)
label.textAlignment = .center
label.adjustsFontSizeToFitWidth = true
return label
}()
let doneButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Register", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 27.5, weight: .heavy)
button.backgroundColor = #colorLiteral(red: 1, green: 0.6749386191, blue: 0.7228371501, alpha: 1)
button.heightAnchor.constraint(equalToConstant: 60).isActive = true
button.widthAnchor.constraint(equalToConstant: 60).isActive = true
button.titleLabel?.adjustsFontForContentSizeCategory = true
button.layer.cornerRadius = 22
button.addTarget(self, action: #selector(handleDone), for: .touchUpInside)
return button
}()
lazy var selectPhotoButtonWidthAnchor = selectPhotoButton.widthAnchor.constraint(equalToConstant: 275)
lazy var selectPhotoButtonHeightAnchor = selectPhotoButton.heightAnchor.constraint(equalToConstant: 275)
#objc func handleSelectPhoto() {
let alert = UIAlertController(title: "Access your photos", message: "Can Crusht open your photos so you can select a profile picture?", preferredStyle: .alert)
let action = UIAlertAction(title: "Yes", style: .default){(UIAlertAction) in
// let imagePickerController = UIImagePickerController()
// imagePickerController.delegate = self
// self.present(imagePickerController, animated: true)
let imagePicker = CustomImagePickerController()
imagePicker.delegate = self
imagePicker.imageBttn = self.selectPhotoButton
self.present(imagePicker, animated: true)
}
let cancel = UIAlertAction(title: "No", style: .cancel, handler: nil)
alert.addAction(action)
alert.addAction(cancel)
self.present(alert, animated: true, completion: nil)
return
}
override func viewDidLoad() {
super.viewDidLoad()
setupGradientLayer()
let stack = UIStackView(arrangedSubviews: [selectPhotoButton, doneButton])
view.addSubview(stack)
stack.axis = .vertical
view.addSubview(label)
label.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor, padding: .init(top: view.bounds.height/9, left: 30, bottom: 0, right: 30))
stack.anchor(top: label.bottomAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 4, left: 30, bottom: view.bounds.height/5, right: 30))
stack.spacing = 15
view.addSubview(errorLabel)
errorLabel.isHidden = true
errorLabel.anchor(top: stack.bottomAnchor, leading: view.leadingAnchor, bottom: nil, trailing: view.trailingAnchor, padding: .init(top: 10, left: 20, bottom: 0, right: 20))
}
#objc fileprivate func handleDone(completion: #escaping (Error?) ->()) {
if imageFull == false {
errorLabel.isHidden = false
return
}
else {
handleRegister()
}
}
var phone: String!
var gender = String()
var sexYouLike = String()
let animationView = AnimationView()
fileprivate func handleRegister() {
print("whatever")
}
fileprivate func saveImageToFirebase(profImage: UIImage) {
view.addSubview(animationView)
animationView.fillSuperview()
let filename = UUID().uuidString
let ref = Storage.storage().reference(withPath: "/images/\(filename)")
guard let imageData = profImage.jpegData(compressionQuality: 0.75) else {return}
ref.putData(imageData, metadata: nil) { (nil, err) in
if let err = err {
print(err)
return
}
ref.downloadURL(completion: { (url, err) in
if let err = err {
print(err)
return
}
let imageUrl = url?.absoluteString ?? ""
self.saveInfoToFirestore(imageUrl: imageUrl)
})
}
}
fileprivate func saveInfoToFirestore(imageUrl: String){
let uid = Auth.auth().currentUser?.uid ?? ""
let docData: [String: Any] =
[
"ImageUrl1": imageUrl,
]
//let userAge = ["Age": age]
Firestore.firestore().collection("users").document(uid).setData(docData, merge: true) { (err) in
if let err = err {
print(err)
return
}
let customtabController = CustomTabBarController()
self.animationView.removeFromSuperview()
self.present(customtabController, animated: true)
}
}
I simply want the app to save the image and move on to the home tab controller
Related
I want to create a custom alert via code. This code below is working, but I Need help on some questions about passing data and retain cycles
is it right my usage of [weak self] ?
I'd like to avoid Delegate Pattern, so my plan is to pass an action as handler, this should keep Controller clean and make code more reusable. Is mine a proper solution?
In my mind, a view should not be "auto removing" but its parent should remove it, so I'd like to pass a reference to the parent controller in order to comunicate via completion, but it seems to create a retain circle (deinit never called), so I'm doing this:
self?.removeFromSuperview() //works, but not sure it is right
//self?.parentController.removeFromParent() //never calls deinit
I had a problem passing closure as parameter inside an init with cgrect as parameter. Is there a proper way other than this solution to handle that?
required init(refersTo: UIViewController, comp: #escaping () -> Void) {
myTransmittedCompletion = comp
self.parentController = refersTo
super.init(frame: CGRect.zero)
}
I call my alert this way
#IBAction func centralButton(_ sender: UIButton) {
let alert = MyAlertInCode(refersTo: self, comp: testPerCOmpletion)
self.view.addSubview(alert)
}
func testPerCOmpletion() {
print("completion")
}
my custom class
class MyAlertInCode: UIView {
let myTransmittedCompletion: () -> Void
let parentController: UIViewController
private let myTitleLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.numberOfLines = 0
v.text = "very long long long logn title"
v.textAlignment = .center
v.font = UIFont.systemFont(ofSize: 28, weight: .bold)
return v
}()
private let mySubtTitleLabel: UILabel = {
let v = UILabel()
v.translatesAutoresizingMaskIntoConstraints = false
v.numberOfLines = 0
v.text = "very long long long logn title"
v.textAlignment = .center
v.font = UIFont.systemFont(ofSize: 20, weight: .bold)
return v
}()
private let myButton: UIButton = {
let v = UIButton()
v.translatesAutoresizingMaskIntoConstraints = false
v.setTitle("Button", for: .normal)
v.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .bold)
v.setTitleColor(.systemBlue, for: .normal)
return v
}()
//white panel of the alert
private let container: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .white
v.layer.cornerRadius = 24
v.backgroundColor = .purple
return v
}()
private lazy var stack: UIStackView = {
let v = UIStackView(arrangedSubviews: [myTitleLabel, mySubtTitleLabel, myButton])
v.translatesAutoresizingMaskIntoConstraints = false
v.axis = .vertical
v.spacing = 10
v.distribution = .fillEqually
v.backgroundColor = .green
return v
}()
required init(refersTo: UIViewController, comp: #escaping () -> Void) {
myTransmittedCompletion = comp
self.parentController = refersTo
super.init(frame: CGRect.zero)
myButton.addTarget(self, action: #selector(methodInsideAlertClass), for: .touchUpInside)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(animateOut)))
self.backgroundColor = UIColor.gray.withAlphaComponent(0.6)
#warning("UIScreen.main.bounds //deprecated in the future, at 14-ott-2022")
// self.frame = UIScreen.main.bounds //deprecated in the future, at 14-ott-2022
guard let windowBoundsFromIOS13 = UIApplication.shared.currentUIWindow()?.rootViewController?.view.bounds else {return}
self.frame = windowBoundsFromIOS13
self.addSubview(container)
NSLayoutConstraint.activate([
container.centerYAnchor.constraint(equalTo: self.centerYAnchor),
container.centerXAnchor.constraint(equalTo: self.centerXAnchor),
container.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.7),
container.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.5),
])
container.addSubview(stack)
NSLayoutConstraint.activate([
stack.heightAnchor.constraint(equalTo: container.heightAnchor, multiplier: 0.6),
stack.centerYAnchor.constraint(equalTo: container.centerYAnchor),
stack.leadingAnchor.constraint(equalTo: container.leadingAnchor),
stack.trailingAnchor.constraint(equalTo: container.trailingAnchor),
])
animateIn()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
print("❌")
}
//MARK: methods
#objc private func methodInsideAlertClass() {
print("methodInsideAlertClass tapped")
}
#objc private func animateOut() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseIn) {
self.container.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
self.alpha = 0
} completion: { [weak self] isCompleted in
if isCompleted {
self?.myTransmittedCompletion()
self?.removeFromSuperview() // shouldn't be removed by parent view?
// self?.parentController.removeFromParent() //never calls deinit
}
}
}
#objc private func animateIn() {
self.container.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
self.alpha = 1
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseIn) {
self.container.transform = .identity
self.alpha = 1
}
}
}
since cannot use Windows:
public extension UIApplication {
func currentUIWindow() -> UIWindow? {
let connectedScenes = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.compactMap { $0 as? UIWindowScene }
let window = connectedScenes.first?
.windows
.first { $0.isKeyWindow }
return window
}
}
pic of database
I am trying to show a label with the user's status on their profile page. after logging in the user gets presented with a VC that has a side menu. on that side menu is a "profile" option. once choosing this they go to their profile controller. right now i simply need to search users/current uid/ "MembershipStatus" and present this result into a label called "welcomeLabel".
I am returning nul
import UIKit
import Firebase
class NonMemberProfileController: UIViewController {
// MARK: - Properties
var welcomeLabel: UILabel = {
let label = UILabel()
label.textColor = .white
label.font = UIFont.systemFont(ofSize: 28)
label.translatesAutoresizingMaskIntoConstraints = false
label.alpha = 0
return label
}()
// MARK: - Init
override func viewDidLoad()
{
super.viewDidLoad()
authenticateUserAndConfigureView()
}
func loadUserData()
{
guard let uid = Auth.auth().currentUser?.uid else {return}
//.child("MembershipStatus")
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value) {
(snapshot) in
if snapshot.hasChild("MembershipStatus"){
print("true we have bingo")
} else {
print("no bueno")
dump(snapshot)
}
guard let status = snapshot.value as? String else { return }
self.welcomeLabel.text = "Welcome, \(status)"
print("this is lkjfdskjklsfad" + status)
UIView.animate(withDuration: 0.5, animations: {
self.welcomeLabel.alpha = 1
})
}
}
func authenticateUserAndConfigureView(){
if Auth.auth().currentUser == nil {
DispatchQueue.main.async {
let navController = UINavigationController(rootViewController: LoginViewController())
navController.navigationBar.barStyle = .black
self.present(navController, animated: true, completion: nil)
}
} else {
configureNavigationBar()
loadUserData()
}
}
// MARK: - Selectors
#objc func handleDismiss() {
dismiss(animated: true, completion: nil)
}
// MARK: - Helper Functions
func configureNavigationBar() {
view.backgroundColor = UIColor.lightGray
navigationItem.title = "Profile"
navigationController?.navigationBar.barTintColor = .darkGray
navigationController?.navigationBar.barStyle = .black
navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Home_2x").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "baseline_settings_white_24dp").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleDismiss))
view.addSubview(welcomeLabel)
welcomeLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
welcomeLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
You are using Cloud Firestore for data storage but your code is reading data form RealTime Database. You have to read data like this:
let userRef = Firestore.firestore().collection("users").document(uid)
userRef.getDocument { (documentSnapshot, error) in guard
let document = documentSnapshot?.data() else {
print(error)
return
}
print(document)
}
My swift code below would like to use a button to go to the first photo in the index. My code works if the user types 0 into the tetxtfield. Then the image view fetches the first item saved in the index. What I want to do is when the user presses rghtBtn, it will have the effect as entering 0.Then if it is pressed again it fetches the 2nd item in the index. So it is constant increasing the index. The user is fetching a binary data called pic. I have a photo of binary data.
import UIKit
import CoreData
class ViewController: UIViewController,UITextFieldDelegate {
lazy var context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var itemsName : [NSManagedObject] = []
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = (textField.text as? NSString)?.replacingCharacters(in: range, with: string), let index = Int(text) else {
return true
}
loadImage(at: index - 1 )
// currentPageLabel.text = tt.text
let ee = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
return true
}
func loadImage(at index : Int) {
let fetchRequest = NSFetchRequest<Users>(entityName: "Users")
fetchRequest.predicate = NSPredicate(format: "idx == %d", Int32(index))
do {
if let user = try context.fetch(fetchRequest).first {
pic.image = UIImage(data: user.pic!)
} else {
pic.image = UIImage(named: "np.png")
}
} catch {
print("Could not fetch \(error) ")
}
}
var pic = UIImageView()
var tt = UITextField()
var lzbel = UILabel()
var submit = UIButton()
var leftBtn = UIButton()
var rightBtn = UIButton()
override var prefersStatusBarHidden: Bool {
return true
}
override func viewDidLoad() {
super.viewDidLoad()
let item = Users(context: context)
let vex = UIImage(named: "lili.jpg")!.pngData()
item.pic = vex
item.idx = 0
let item2 = Users(context: context)
let vex2 = UIImage(named: "camila.jpg")!.pngData()
item2.pic = vex2
item2.idx = 1
print("Storing Data..")
do {
try context.save()
} catch {
print("Storing data Failed", error)
}
lzbel.textAlignment = .center
tt.placeholder = "Enter Page Number"
tt.delegate = self
view.backgroundColor = .white
[pic,tt,rightBtn].forEach{
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
$0.backgroundColor = .green
}
pic.backgroundColor = .red
rightBtn.setTitle(">", for: .normal)
tt.textAlignment = .center
NSLayoutConstraint.activate ([
tt.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
tt.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
tt.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.3, constant: 0),
tt.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
tt.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 7/7, constant: 0),
pic.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
pic.topAnchor.constraint(equalTo: tt.bottomAnchor, constant : 0),
pic.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.33, constant: 0),
pic.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
rightBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant :0),
rightBtn.topAnchor.constraint(equalTo: pic.bottomAnchor, constant : 0),
rightBtn.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.33, constant: 0),
rightBtn.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant : 0),
])
tt.backgroundColor = .systemGray
submit.backgroundColor = .purple
submit.setTitle("Save", for: .normal)
let appD = UIApplication.shared.delegate as! AppDelegate
let context = appD.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Users")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "idx", ascending: true)]
do {
itemsName = try context.fetch(fetchRequest)
}catch {
print("Ashley Tisdale")
}
lzbel.text = String(itemsName.count) + String(" total pages saved")
lzbel.backgroundColor = .red
rightBtn.addTarget(self, action: #selector(rightBtnPress), for: .touchDown)
}
#objc func rightBtnPress(){
//Hiting this code should have the effect
}
}
You need an index variable.
var counter = 0
Then in rightBtnPress load the image at index counter and increment the variable.
#objc func rightBtnPress() {
loadImage(at: counter)
counter += 1
}
I have a videPlayerView which has ContainerView on top of it to show activityIndicatorView. The view gets loaded when i select an item from a collectionView inside a cell. My question is how to remove this view using the cancelButton, i tried removeFromSuperview() for both the container and the playerView but the app crashes with this error
AQDefaultDevice (1): skipping input stream 0 0 0x0
Here is the code:
class VideoPlayerView: UIView {
var videoUrl: String!
var uuidd: String!
let activityIndicatorView: UIActivityIndicatorView = {
let aiv = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
aiv.translatesAutoresizingMaskIntoConstraints = false
aiv.startAnimating()
return aiv
}()
lazy var controlsContainerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(white: 0, alpha: 1)
view.isUserInteractionEnabled = true
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handlezoomout)))
return view
}()
func handlezoomout(hh: UITapGestureRecognizer){
print("n3me")
}
lazy var cancelButton: UIButton = {
let cancelButton = UIButton()
cancelButton.setImage(#imageLiteral(resourceName: "cancel"), for: UIControlState())
cancelButton.addTarget(self, action: #selector(cancel), for: .touchUpInside)
return cancelButton
}()
func cancel() {
controlsContainerView.removeFromSuperview()
let video = VideoPlayerView()
video.removeFromSuperview()
}
lazy var pausePlayButton: UIButton = {
let button = UIButton(type: .system)
let image = UIImage(named: "pause")
button.setImage(image, for: UIControlState())
button.translatesAutoresizingMaskIntoConstraints = false
button.tintColor = .white
button.isHidden = true
button.addTarget(self, action: #selector(handlePause), for: .touchUpInside)
return button
}()
var isPlaying = false
func handlePause() {
if isPlaying {
player?.pause()
pausePlayButton.setImage(UIImage(named: "play"), for: UIControlState())
} else {
player?.play()
pausePlayButton.setImage(UIImage(named: "pause"), for: UIControlState())
}
isPlaying = !isPlaying
}
override init(frame: CGRect) {
super.init(frame: frame)
setUpPlayerView()
controlsContainerView.frame = frame
addSubview(controlsContainerView)
cancelButton.frame = CGRect(x: 16.0, y: 20.0, width: 30.0, height: 30.0)
controlsContainerView.addSubview(cancelButton)
controlsContainerView.addSubview(activityIndicatorView)
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
controlsContainerView.addSubview(pausePlayButton)
pausePlayButton.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
pausePlayButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
pausePlayButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
pausePlayButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
backgroundColor = UIColor.black
}
var player: AVPlayer?
fileprivate func setUpPlayerView() {
let postQuery = PFQuery(className: "posts")
postQuery.whereKey("uuid", equalTo: PostUuidGlobalVariable.postuuid.last!)
postQuery.getFirstObjectInBackground { (object, error) in
if (error == nil && object != nil) {
let videoFile = object!["video"] as! PFFile
if let url = URL (string: videoFile.url!) {
self.player = AVPlayer(url: url)
let playerLayer = AVPlayerLayer(player: self.player)
self.layer.addSublayer(playerLayer)
playerLayer.frame = self.frame
self.player?.play()
self.player?.isMuted = false
self.player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.playerDidFinishPlaying(note:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
}
}
}
}
func playerDidFinishPlaying(note: NSNotification) {
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
//this is when the player is ready and rendering frames
if keyPath == "currentItem.loadedTimeRanges" {
activityIndicatorView.stopAnimating()
pausePlayButton.isHidden = false
isPlaying = true
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class VideoLauncher: NSObject {
func showVideoPlayer() {
if let keyWindow = UIApplication.shared.keyWindow {
let view = UIView(frame: keyWindow.frame)
view.isUserInteractionEnabled = true
view.backgroundColor = UIColor.white
view.frame = CGRect(x: keyWindow.frame.width - 10, y: keyWindow.frame.height - 10, width: 10, height: 10)
let height = keyWindow.frame.height
let videoPlayerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: height)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
view.addSubview(videoPlayerView)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
view.frame = keyWindow.frame
}, completion: { (completedAnimation) in
//maybe we'll do something here later...
UIApplication.shared.isStatusBarHidden = true
})
keyWindow.addSubview(view)
}
}
}
There is a chance this is related to you changing the user interface outside of the main thread.
From UIView documents
'Threading Considerations
Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the
UIView
class from code running in the main thread of your application. The only time this may not be strictly necessary is when creating the view object itself, but all other manipulations should occur on the main thread.'
Also your cancel function creates a new video player view and then tries to remove it from it parent which doesn't look correct.
Your cancel callback should probably be as follows
func cancel() {
DispatchQueue.main.async { [unowned self] in
// to remove controls
self.controlsContainerView.removeFromSuperview()
// to remove video player view
self.view.removeFromSuperview()
}
}
I am in the process of creating a game (Swift) in xcode using a number of SKScene and Sprite objects. I want to create a Scene (settings scene) that captures the player's name, email, gender etc. How can I go about this? How can I capture input from user. SKScenes do not allow input fields/values in the UI?
Thanks
You can build a custom login page that is conform with your game layout without try to rebuild in UIKit the same graphic assets.
Few days ago I've written an answer about SKSceneDelegate to communicate between the scene(SpriteKit) and the viewController (UIKit), take present this answer if you want to call other viewControllers because its the same concept of this answer..
Starting with this GameViewController we can develop some useful methods to handle the login form buttons and show some alerts:
import UIKit
import SpriteKit
class GameViewController: UIViewController, TransitionDelegate {
override func viewDidLoad() {
super.viewDidLoad()
guard let view = self.view as! SKView? else { return }
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
let scene = GameScene(size:view.bounds.size)
scene.scaleMode = .fill
scene.delegate = self as TransitionDelegate
scene.anchorPoint = CGPoint.zero
view.presentScene(scene)
}
func showAlert(title:String,message:String) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default) { action in
print("handle Ok action...")
})
alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(alertController, animated: true)
}
func handleLoginBtn(username:String,password:String) {
print("handleLoginBtn")
print("username is: \(username) and password: \(password)")
}
func handleFacebookBtn() {
print("handleFacebookBtn")
}
func handleTwitterBtn() {
print("handleTwitterBtn")
}
}
Then we can make our scene trying to take the advantage of SpriteKit elements:
import SpriteKit
import UIKit
protocol TransitionDelegate: SKSceneDelegate {
func showAlert(title:String,message:String)
func handleLoginBtn(username:String,password:String)
func handleFacebookBtn()
func handleTwitterBtn()
}
class GameScene: SKScene,UITextFieldDelegate {
var usernameTextField:UITextField!
var passwordTextField:UITextField!
var loginBtn:SKShapeNode!
var facebookBtn:SKShapeNode!
var twitterBtn:SKShapeNode!
override func didMove(to view: SKView) {
//bg
let bg = SKSpriteNode(imageNamed: "appleWallpaper")
addChild(bg)
bg.position = CGPoint(x:self.size.width/2,y:self.size.height/2)
//title
let title = SKLabelNode.init(fontNamed: "AppleSDGothicNeo-Bold")
title.text = "xyzGame"; title.fontSize = 25
title.fontColor = .orange
addChild(title)
title.zPosition = 1
title.position = CGPoint(x:self.size.width/2,y:self.size.height-80)
//textfields
guard let view = self.view else { return }
let originX = (view.frame.size.width - view.frame.size.width/1.5)/2
usernameTextField = UITextField(frame: CGRect.init(x: originX, y: view.frame.size.height/4.5, width: view.frame.size.width/1.5, height: 30))
customize(textField: usernameTextField, placeholder: "Enter your username")
view.addSubview(usernameTextField)
usernameTextField.addTarget(self, action:#selector(GameScene.textFieldDidChange(textField:)), for: UIControlEvents.editingChanged)
passwordTextField = UITextField(frame: CGRect.init(x: originX, y: view.frame.size.height/4.5+60, width: view.frame.size.width/1.5, height: 30))
customize(textField: passwordTextField, placeholder: "Enter your password", isSecureTextEntry:true)
view.addSubview(passwordTextField)
//buttons
let myBlue = SKColor(colorLiteralRed: 59/255, green: 89/255, blue: 153/255, alpha: 1)
loginBtn = getButton(frame: CGRect(x:self.size.width/4,y:self.size.height/2,width:self.size.width/2,height:30),fillColor:myBlue,title:"Login",logo:nil,name:"loginBtn")
addChild(loginBtn)
loginBtn.zPosition = 1
let label = SKLabelNode.init(fontNamed: "AppleSDGothicNeo-Regular")
label.text = "or connect with"; label.fontSize = 15
label.fontColor = .gray
addChild(label)
label.zPosition = 1
label.position = CGPoint(x:self.size.width/2,y:self.size.height/2-30)
let logoFb = SKSpriteNode.init(imageNamed: "facebook-icon")
logoFb.setScale(0.5)
facebookBtn = getButton(frame: CGRect(x:self.size.width/4,y:self.size.height/2-80,width:self.size.width/4.5,height:30),fillColor:myBlue,logo:logoFb,name:"facebookBtn")
addChild(facebookBtn)
facebookBtn.zPosition = 1
let myCyan = SKColor(colorLiteralRed: 85/255, green: 172/255, blue: 239/255, alpha: 1)
let logoTw = SKSpriteNode.init(imageNamed: "twitter-icon")
logoTw.setScale(0.5)
twitterBtn = getButton(frame: CGRect(x:self.size.width/2,y:self.size.height/2-80,width:self.size.width/4.5,height:30),fillColor:myCyan,logo:logoTw,name:"twitterBtn")
addChild(twitterBtn)
twitterBtn.zPosition = 1
}
func customize(textField:UITextField, placeholder:String , isSecureTextEntry:Bool = false) {
let paddingView = UIView(frame:CGRect(x:0,y: 0,width: 10,height: 30))
textField.leftView = paddingView
textField.keyboardType = UIKeyboardType.emailAddress
textField.leftViewMode = UITextFieldViewMode.always
textField.attributedPlaceholder = NSAttributedString(string: placeholder,attributes: [NSForegroundColorAttributeName: UIColor.gray])
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.layer.borderColor = UIColor.gray.cgColor
textField.layer.borderWidth = 0.5
textField.layer.cornerRadius = 4.0
textField.textColor = .white
textField.isSecureTextEntry = isSecureTextEntry
textField.delegate = self
}
func getButton(frame:CGRect,fillColor:SKColor,title:String = "",logo:SKSpriteNode!,name:String)->SKShapeNode {
let btn = SKShapeNode(rect: frame, cornerRadius: 10)
btn.fillColor = fillColor
btn.strokeColor = fillColor
if let l = logo {
btn.addChild(l)
l.zPosition = 2
l.position = CGPoint(x:frame.origin.x+(frame.size.width/2),y:frame.origin.y+(frame.size.height/2))
l.name = name
}
if !title.isEmpty {
let label = SKLabelNode.init(fontNamed: "AppleSDGothicNeo-Regular")
label.text = title; label.fontSize = 15
label.fontColor = .white
btn.addChild(label)
label.zPosition = 3
label.position = CGPoint(x:frame.origin.x+(frame.size.width/2),y:frame.origin.y+(frame.size.height/4))
label.name = name
}
btn.name = name
return btn
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
let positionInScene = touch!.location(in: self)
let touchedNode = self.atPoint(positionInScene)
if let name = touchedNode.name {
switch name {
case "loginBtn":
self.run(SKAction.wait(forDuration: 0.1),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).handleLoginBtn(username:self.usernameTextField.text!,password: self.passwordTextField.text!)
})
case "facebookBtn":
self.run(SKAction.wait(forDuration: 0.1),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).handleFacebookBtn()
})
case "twitterBtn":
self.run(SKAction.wait(forDuration: 0.1),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).handleTwitterBtn()
})
default:break
}
}
}
func textFieldDidChange(textField: UITextField) {
//print("everytime you type something this is fired..")
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == usernameTextField { // validate email syntax
let emailRegEx = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
let result = emailTest.evaluate(with: textField.text)
let title = "Alert title"
let message = result ? "This is a correct email" : "Wrong email syntax"
if !result {
self.run(SKAction.wait(forDuration: 0.01),completion:{[unowned self] in
guard let delegate = self.delegate else { return }
(delegate as! TransitionDelegate).showAlert(title:title,message: message)
})
}
}
}
deinit {
print("\n THE SCENE \((type(of: self))) WAS REMOVED FROM MEMORY (DEINIT) \n")
}
}
Output:
Animated output:
As you can see we can handle both framework with their delegate methods, I've tested this page with iPhone 5 and iPhone 7 plus.