I want to run UIActivityIndicator when the application is opened. How can I do this? My code is the way I share it down there. I want this code to work when Launchscreen opens.
#objc func buttonTapped(_ sender: UIButton) {
let size = CGSize(width: 30, height: 30)
let selectedIndicatorIndex = sender.tag
let indicatorType = presentingIndicatorTypes[selectedIndicatorIndex]
startAnimating(size, message: "Loading...", type: indicatorType, fadeInAnimation: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5) {
NVActivityIndicatorPresenter.sharedInstance.setMessage("Authenticating...")
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
self.stopAnimating(nil)
}
}
for (index, indicatorType) in presentingIndicatorTypes.enumerated() {
let x = 150
let y = 250
let frame = CGRect(x: x, y: y, width: 100, height: 100)
let activityIndicatorView = NVActivityIndicatorView(frame: frame,
type: .orbit)
let animationTypeLabel = UILabel(frame: frame)
activityIndicatorView.padding = 20
if indicatorType == NVActivityIndicatorType.orbit {
activityIndicatorView.padding = 0
}
self.view.addSubview(activityIndicatorView)
self.view.addSubview(animationTypeLabel)
activityIndicatorView.startAnimating()
let button: UIButton = UIButton(frame: frame)
button.tag = index
#if swift(>=4.2)
button.addTarget(self,
action: #selector(buttonTapped(_:)),
for: .touchUpInside)
#else
button.addTarget(self,
action: #selector(buttonTapped(_:)),
for: UIControlEvents.touchUpInside)
#endif
self.view.addSubview(button)
}
What you are trying to achieve is to show UIActivityIndicator during launch screen, which is unfortunately NOT possible. If you want to do any animations on launch screen, use your first view controller as splash screen and redirect to main screen when animation on splash screen ends.
Some ideas:
Create separate loading page and call it from didFinishLaunchingWithOptions method in App delegate
Add loading indicator (can be whatever you want) to it
Set timer of 2-3 seconds, when it ends redirect to your main screen
P.S. delaying application launch just to show users some awesome animation/loading screen is not a good idea. For a first time app users it might seem cool and such, but with an extensive use of an application users might get frustrated waiting even few seconds each time an application is launched.
Good luck :)
Related
I add a custom button to the tabBar in my MyViewController.viewDidLoad(subclass of UITabBarController)
But I find it doesn't response the selector.
If I delay one second to add button(in DispatchQueue.main.asyncAfter closure) ,it works OK.
I think it's not the right way to resolve it.
func addButton() {
let button = UIButton(type: UIButton.ButtonType.custom)
button.bounds = CGRect(x:0,y:0,width:30,height:30);
button.backgroundColor = UIColor.red
button.center = CGPoint(x:self.tabBar.frame.size.width/2, y:self.tabBar.frame.size.height/2 - 20);
button.addTarget(self, action: #selector(click(button:)), for: UIControl.Event.touchUpInside)
tabBar.addSubview(button)
}
You have added button to UITabBar of UITabBarController as half of part of the button would appear above the Tabbar and half of below the Tabbar as per frame.
So I guess you will not get click on part of that button which is out of Tabbar(above Tabbar) would not get touch. I you will make button little big OR try to click with arrow in simulator, you will get idea.
If you need to have button at bottom but slightly upper, then please create custom Tabbar to achieve design like this. Or else you can add that button into UITabBarController’s view instead of Tabbar.
class MyTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.addButton()
}
func addButton() {
let button = UIButton(type: UIButton.ButtonType.custom)
button.bounds = CGRect(x:0,y:0,width:50,height:50); //1
button.backgroundColor = UIColor.purple
button.center = CGPoint(x:self.tabBar.frame.size.width/2, y:self.tabBar.frame.size.height/2 - 50 + self.tabBar.frame.origin.y); //2
button.addTarget(self, action: #selector(click(button:)), for: UIControl.Event.touchUpInside)
button.layer.cornerRadius = button.frame.size.height/2
button.layer.masksToBounds = false
button.layer.shadowColor = UIColor.black.withAlphaComponent(0.5).cgColor
button.layer.shadowRadius = 5.0
button.layer.shadowOffset = CGSize(width: 0.0, height: 5.0)
button.layer.shadowOpacity = 0.5
//tabBar.addSubview(button) //3
self.view.addSubview(button). //4
}
#objc func click(button: UIButton) {
print("Button get clicked")
}
}
I have marked four things with commented by numbers at the end of lines, that you can make to your code and try.
I have read the manual (https://material.io/develop/ios/components/buttons/), but still have not understood how to do it.
class FloatingButtonController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let floatingButton = MDCFloatingButton()
floatingButton.setImage( UIImage(named: "plus"), for: .normal)
floatingButton.backgroundColor = .white
floatingButton.setElevation(ShadowElevation(rawValue: 6), for: .normal)
floatingButton.addTarget(self, action: #selector(btnFloatingButtonTapped(floatingButton:)), for: .touchUpInside)
self.view.addSubview(floatingButton)
}
#objc func btnFloatingButtonTapped(floatingButton: MDCFloatingButton){
floatingButton.collapse(true) {
floatingButton.expand(true, completion: nil)
}
}
}
My project on the screen. As you can see - the button is missing on the screen. When you click on a blank screen errors appear.
Button touch target does not meet minimum size guidlines of (48, 48). Button: >, Touch Target: {0, 0}
Screenshot of my project
Tell me, please, what am I doing wrong? And how do I get the job right?
You didn't set the frame to button. You need to specify (x, y) position to place button in view.
floatingButton.frame = CGRect(x: 300, y: 300, width: 48, height: 48)
To add button target the button should have size atlease 48x48.
You could constraint it manually or set frame.
OR you could add button via Storyboard too.
i need to have a custom view controller to popup independent of currently presenting view controller,
it should show every 30 seconds, and when user clicks on popup, it should hide or dismiss.
i have been searching but nothing worked , Any help will be appreciated.
Thanks
Write logic in AppDelegate. It is a singleton class and instance of it stays in memory for the entire life cycle of App and you can present view controller using
UIApplication.shared.keyWindow?.rootViewController?.present(yourVC, animated: true, completion: nil)
You need not know which view controller is on top currently :)
It's working for me try this and let me know:
let imageV: UIImageView = UIImageView(frame: CGRectMake(90, 0, 80, 80))
imageV.image = UIImage(named: "stop_record")!
self.message = UIAlertView(title: “Custom view”, message: “Processing…”, delegate: self, cancelButtonTitle:nil)
let v: UIView = UIView(frame: CGRectMake(10, 20, 100, 100))
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(SONGS.new(_:)))
tapGestureRecognizer.numberOfTapsRequired = 1
if tapGestureRecognizer.numberOfTapsRequired == 1
{
imageV.userInteractionEnabled = true
imageV.addGestureRecognizer(tapGestureRecognizer)
v.addSubview(imageV)
self.timer.invalidate()
}
self.timer = NSTimer.scheduledTimerWithTimeInterval(180.0, target: self, selector: #selector(SONGS.displayAlert), userInfo: nil, repeats: false)
let label = UILabel(frame: CGRectMake(90, 40, 100, 101))
label.textAlignment = NSTextAlignment.Center
self.message.setValue(v, forKey: "accessoryView")
self.message.tag = 3
self.message.show()
func displayAlert
{
self.message.dismissWithClickedButtonIndex(0,animated: true)
}
For showing pop up after every 30 seconds, you should use NSTimer with interval of 30 seconds. After every 30 secs a selector(say showPopUp) method is called which you specify while initialising NSTimer. In this showPopUp you should show popup you can use UIAlertController and add that on window.
All the above implementation should be done in Appdelegate.m
I'm going to jump straight to the point. My App has crashed and I have no clue what's wrong with it. I thing that is most likely incorrect is the part with the screen size but I don't really think so. Here are my screen size variables:
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width - CGFloat(screenSize.width)
let screenHeight = screenSize.height - CGFloat(screenSize.height)
They are being used here, in my tick from my NSTimer:
func tick()
{
let color = Int(arc4random_uniform(3))
if(color == 1)
{
let newDrop:Drop = Drop(dropType: DropType.blue, xPos: Int(arc4random_uniform(UInt32(screenWidth - 5))))
drops.append(newDrop)
} else if(color == 2)
{
let newDrop:Drop = Drop(dropType: DropType.red, xPos: Int(arc4random_uniform(UInt32(screenHeight - 5))))
drops.append(newDrop)
}
drawDrops(drops)
}
The drawDrops function is:
func drawDrops(drops:[Drop])
{
for(var i = 0; i < drops.count; i++)
{
print("Hello, World!")
let button:UIButton = UIButton()
if(drops[i].dropType == DropType.red)
{
button.setImage(UIImage(named: "RedDrop.png"), forState: UIControlState.Normal)
button.addTarget(self, action: Selector("redPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
} else if(drops[i].dropType == DropType.blue)
{
button.setImage(UIImage(named: "BlueDrop.png"), forState: UIControlState.Normal)
button.addTarget(self, action: Selector("bluePressed:"), forControlEvents: UIControlEvents.TouchUpInside)
}
button.frame = CGRectMake(CGFloat(drops[i].xPos), 10, 20, 20)
self.view.addSubview(button)
}
}
Does anyone have any ideas? I'm getting this error that I've never even heard of and have no idea what to do or change:
2016-02-03 18:55:26.679 #Project Name#[19295:1104595] Unknown class Mai
in Interface Builder file. (lldb)
Thanks!
-Jack
EDIT:
I figured out the part with the 'Mai' but now I'm getting a crash with just 'llbd.' The console literally doesn't say anything else:
I'm pretty sure it's something in the tick() function because it takes a second to crash when it enters the view controller and my NSTimer is set to 1 second...
(Not sure if this helps or has to do with anything but it has this line selected as EXC_BAD_INSTRUCTION:
let newDrop:Drop = Drop(dropType: DropType.red, xPos: Int(arc4random_uniform(UInt32(screenHeight - 5))))
)
Answer?
Don't know why this worked but I pulled out the arc4random from he Drop object and made it into its own variable. I don't know why this worked but in happy. :-)
Your crash refers to Interface Builder. I suspect you've got a view with a custom class that no longer exists (or you've mis-typed it - I don't know what 'Mai' is).
Find which view it is and fix the issue in the 'Identity Inspector'. Going by the error message, it sounds like it doesn't have anything to do with the code you posted.
I am trying to get a UIButton to repeat the code over and over until the user releases the button. I have an up arrow in a game and when it is tapped, a space ship will go up until it is released. Here is the code for my button:
//at top of page
let upArrow = UIButton()
//in viewdidload()
let upArrowImage = UIImage(named: "upArrow") as UIImage?
upArrow.setImage(upArrowImage, forState: .Normal)
upArrow.frame = CGRectMake(10, 220, 90, 40)
upArrow.addTarget(self, action: "upArrowTouched:", forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(upArrow)
//outside of viewDidLoad()
func upArrowTouched(sender:UIButton!){
spaceship.position = CGPointMake(spaceship.position.x, spaceship.position.y + 3)
}
Any suggestions??
You need to implement your own solution for this, something along the lines
1- User touches down (UIControlEvent touchDown), you start a time that fires every x secs/millisecs
2- Timer will fire the action over and over
3- User touches up UICOntrolEvent touchUp, you cancel your timer
So you bind those 2 events to different functions, and start/kill your timers with appropriate actions
How that helps
Daniel