Can I use multiple animations in 1 view ios swift - ios

I am trying to create an aquarium style app where the fish and bubbles move once the app is opened using swift and xcode 6.2. The code I am using only allows me to use it once. If I try to use it more the once the animations dont work correctly, they become jumpy and if I try it 3 or more times the animations disappear completely from the app. Does anybody know what code I could use to achieve what I am after or how to alter the code I already have to achieve this. The app is a single view with up to 8 animations. Here is the code I have been using with the (func performBubbles1) and (imgBubbles1) being changed accordingly to match what image view is going to be animated and with what animation.
timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("performBubbles1"), userInfo: nil, repeats: true)
}
func performBubbles1() {
counter++
if (counter==15) {
counter = 1
}
imgBubbles1.image = UIImage(named: "bubbles\(counter).png")
I am new to coding so apologise if this makes no sense or if this has been answered. I have not been able to find the answer I am after.
Thankyou

The approach you're using was not designed to be used that way. You don't want to use NSTimer to essentially act as a render loop. It isn't accurate enough. Additionally, UIKit isn't really designed to swap images like you are at a high frame rate for a variety of different reasons.
What you're actually looking for is SpriteKit. It is designed for making games, but will fit your use case very VERY well. Here is a great SpriteKit tutorial to get you started.
Sprite Kit Swift Tutorial for Beginners

Related

Buttons in iOS app don't respond to touches around the edge of the screen

I'm relatively new to iOS development, but I'm having a go at working on some open source code for an old game that used to be pretty popular (Eden World Builder)
I've made quite a lot of progress in cleaning up the codebase, making small changes. But there's an issue I can't seem to fix. Every button in the game will not respond to taps around the edges of the screen. If a button is in the corner of the screen, you will have to tap towards the bottom of the button.
I've tried to move the buttons away from the edges, and they work, but that isn't practical for use. So there's something preventing the edges of the screen from registering button taps for some reason, and it doesn't seem to be anything to do with the button target areas themselves.
One thing I've noticed: This game is currently on the App Store, even though it hasn't been updated since 2015. In the App Store version (Which is built from the same code that I have) the issue doesn't occur. It must be something to do with building it in a newer version of Xcode, right?
Any assistance would be very helpful, this has been frustrating me for weeks now. Thanks
The answer to your first question is most likely either that your button is outside its parent view, or a gesture recognizer is interfering.
If a button extends beyond its parent views boundaries (or any of its higher parents' boundaries), it will still be visible as long as the parent doesn't have clipping enabled. The result is that you will still see the button, but it will only respond when touching the parts that are inside the parent view. You can find this visually by using Xcode View UI Hierarchy found in the Debug Navigator.
If it is gesture recognizers that interfere with your button, there are several solutions that might work. Several are described in the link you got from #Anbu in the comment.
The answer to your second question is that old apps are linked against old frameworks. Even if they run on the latest iOS version, they still pull in older versions of the framework, causing them to (mostly) continue work as before. This is done to keep compatibility with legacy code.
Try adding this to viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let window = view.window,
let recognizers = window.gestureRecognizers {
recognizers.forEach { r in
r.delaysTouchesBegan = false
r.cancelsTouchesInView = false
r.isEnabled = false
}
}
}

UISlider to change UIImage brightness/contrast in Swift Playground is very laggy.

I am currently creating a filter app. The user has the option to change brightness, contrast, etc.
I have implemented the ability to change these attributes of the image and it currently works but when I test this in Xcode Playgrounds the slider is extremely slow and makes Xcode very laggy.
I assume this is because I have written this in a very inefficient way. I don't want to copy and paste all of my code to Stackoverflow (I want to avoid making this too complicated) so I have uploaded it on Github. Located Here if you download the repo and open the playground up it is 100% running and if you test the slider you will see it is very laggy.
I think what is making it so laggy is that I am reseting the image view every time the slider value is changed resulting in a new UIImage being assigned to it every fraction of a second. Here is a tiny snippet of what I I just said, in code. You will probably still have to look at the code I posted on github since I made some protocols and classes
slider.addTarget(c, action: #selector(c.BrightnessChanged(sender:)), for: .valueChanged)
func updateBrightness(sender:UISlider) {
controls.brightness(sender.value)
img.image = controls.outputUIImage()
}
I genuinely have no intention asking for a solution without having tried myself; I have searched all over the interwebs to figure this out with no success.
Thanks Stackoverflow homies!

IOS GIF cocoapod that allows variable durations and loop counts

Im looking for a cocoapod that enables me to use an animation (gif file) that is upto 200 frames, in an efficient manner
Further it needs to be able to:
start/stop animation
set a duration that the animation should run for
Set the number of iterations the animation should run for
So far I have tried FLAnimatedImage and YLGIFImage , and neither appear to satisfy the above constraints while both are excellent for running through gifs in an efficient manner
I was able to use FLAnimatedImage to handle my requirements. See below
start/stop animation: handled by implementing suggestions outlined in comments here: https://github.com/Flipboard/FLAnimatedImage/issues/52
set a duration that the animation should run for: fixed by using an online gif editor
Set the number of iterations the animation should run for : Handled by using https://github.com/Flipboard/FLAnimatedImage/pull/60

How do I mix UIKit and SpriteKit?

I am making a game in Swift. I have used both UIKit and SpriteKit, but never in the same app. I was wondering how to leverage the power of both of them (They are going to be a lot of menus). If you knew a tutorial I could use, or could tell me, I would be very appreciative. I also would like to know if there is a shortcut for this in xcode (e.g. storyboards)
I would highly recommend you to stick with SpriteKit and to create menus etc with SKNodes. I know that it is really seductive to use UIKit in a game for buttons etc. but you've got many possibilities right in SpriteKit which replaces UIKit-elements.
For example you can use an SKLabelNode instead of an UILabel. Also you've got many more possibilities in SpriteKit to make your menu 'smooth' (For example with SKTransition or SKAction).
Also an example for a menubutton would be:
//Button
var playButton = SKSpriteNode(imageNamed: "yourImage.png")
playButton.position = CGPointMake(300, 300)
playButton.name = "playButton"
addChild(playButton)
Then in your touchesBegan method you can handle that button touch.
Also one very important point to only use SpriteKit is, that you can port a iOS game to OSX within a few hours.

Suspend redrawing while changing multiple children

I have a UIViewController that after asynchronously loading some data needs to resize some child views (Some UITableViews, and some UIScrollViews). This works just fine in the simulator, but on an actual device, it hangs for a long time (as much as 30 seconds in one case). I think the problem is that it wants to recalculate everything after each changed to the child view and I would like to be able to tell it to defer any recalculations until I've resized everything, but I'm drawing a blank on finding a way to do that. Is there a mechanism to do this?
So what I'm doing is something like this (I'm using C# with Xamarin but input in Obj-C would also be appreciated!):
// Need to suspend layout here...
MyTable.Frame = new RectangleF(...);
SomeOtherTable.Frame = new RectangleF(...);
ScrollView.ContentSize = new SizeF(...);
AnotherScrollView.ContentSize = new SizeF(...);
AnotherScrollView.Frame = new RectangleF(...);
// Ok - now you can redraw!
Update: Jacob's suggestion of disabling animation didn't seem to help, but I think I've isolated the problem to the table resizing. They seem to have the biggest impact on performance, but I'm not sure why, or how to mitigate the problem. I may follow up with a separate question on that.

Resources