How To Move Continiously Multiple UIButtons in Specific Directions? - iphone

I have five UIButtons in my application.All these UIButtons are stored in Array.As my Code Show below
NSMutableArray *Buttons = [NSMutableArray arrayWithObjects:btn1, btn2, btn3,btn4,btn5, nil];
Now i Want to move all these UIButtons in Specific Direction Continiously.As my image show below
I Already did some animation in CAkeyframeAnimation,but As a iOS beginer its difficult for me to perform these type of Continious Animation on multiple UIButtons using CAKeyframeAnimation.Can some one help me about this.Any help will be appriated.

I don't know what you've done already, but you could use UIView Animations and then make sure you enable set the UIViewAnimationOptionAllowUserInteraction option.
You can then put a gesture recognizer on the buttons.
To get it to animate, just use UIView's block based animations, there are only 4 that need to be done, and have them repeat. It's fiddly, but it's a quick and dirty solution.

If you are trying to move these buttons around in a way that keeps them reactive to user touches, using Core Animation directly is probably not your best bet. My answer below is based on this assumption.
When a Core Animation-based animation is running, the layer you see moving on screen (the "presentation layer" — see here for more info about this) does not register touches at its current position, but only at the final position of the animation, which is invisible for the user. Thus the moving bouton will behave most of the time as if it is disabled. Though it is possible to hack around this, it is not very convenient and not meant to be used that way.
Another drawback is that CAAnimation is not very flexible once created, for example it would be tedious to stop the movement of the buttons at their current position if you ever needed to.
Instead, you should compute and move your UIButtons frames manually. To synchronize the movement with the display refresh rate and have a smooth animation, use CADisplayLink, which is a very easy to use class that you can use to register a method that will be called back each time the display is being refreshed. In this method you should update your views frames based on the desired behavior of your views.

Related

Forcing a UIImageView to Remain in Front of an Active Animation

Language: Swift, IDE: Xcode for iOS development, Single View Application > View Controller...
I have 2 UIImage Views with identical images that I'm scroll-animating from left to right across the view in order to create a 'slowly-moving background' of sorts. I'd like to place other UI elements (Labels, other images, etc.) in the foreground of this repeating background animation, but find when I run the simulator the foreground image isn't seen...
Question: Is it possible to force other UI elements to stay in front of a repeating animation programmatically?
I'm not at my Mac so I can't share my code at the moment, but if you know a straight answer to the question and/or which method could best achieve this, I'm all ears!!
Thanks in advance! :)
This depends on the z-ordering of the views. Assuming you are adding all of your views then starting the animation call bringSubViewToFront on the view you are animating right before you start animating it. If you are laying things out in interface builder the Z order is based on top = farthest and bottom = closest. If you are adding view programmatically the newest view is always added in front. You can change this with insertSubview:at: and the related methods. Take a look at the documentation for UIView.
If you want z-index use:
YourImageViewName.layer.zPosition = 1
Also you could play with bringToFront method on the parent view:
self.view.bringSubviewToFront(YourImageViewName)
Hope this fix your problem.

continuous/repeating MKMapView

I'm looking for a MKMapView which can be scrolled continuously in any direction and repeats itself. It has to be a MKMapView/subclass and not a custom Map-Framework like route-me.
How to implement that on top of a MKMapview?
I do not know if there's framework already for this, but here's an idea.
Use infinite scroll view sample from wwdc idea and apply it to mapview. So for example, use two mapviews side by side (for infinite scroll along X axis)
Usually one mapview will be shown, but when the dragging goes past the edges of the left/right, then you could slide one mapview out and slide in another mapview.
Your viewcontroller will need to add annotations to both mapviews of course, and if you want to support any direction, that can possibly mean you need 4 map views to cover corner case.
You can probably treat mapview as scrollview when doing this infinite scroll trick, as described in wwdc 2011 scrollview session.
To keep both mapviews in sync, you will need to pass messages between two, for example zoom level. Not sure if mapview has all things you need to observe these things, but do take a look. You could possibly use KVO to observe the internal variable without violating Apple private API check.
Good luck and let me know how it goes.

Drag & sweep with Cocoa on iPhone

I'm about to start a new iPhone app that requires a certain functionality but I'm not sure if it's doable. I'm willing to research but first I just wanted to know if I should at least consider it or not.
I haven't seen this in an app before (which is my main concern, even though I haven't seen too many apps since I don't own an iPhone), but an example would be the iPhone shortcuts panels: you can hold on an app, and then drag it to another panel, sweeping while still dragging it. But this is the core app, is it possible to reproduce something similar within a normal app?
I only need to be sure it can be done before I start digging, I don't need code examples or anything, but if you have some exact resources that you consider helpful, that would be appreciated.
Thanks!
Yes. If you have your custom UIView subclass instance inside a UIScrollView, your view controller just needs to set the UIScrollView to delay content touches and not allow it to cancel touch events.
[scrollView setCanCancelContentTouches:NO];
[scrollView setDelaysContentTouches:YES];
When the user taps and holds in the custom view, the event goes to that custom view, which can process the touch events to drag an item around, but if the user quickly swipes, it scrolls the view.
The "panel" view that you're referring to appears to be a UIPageControl view — although, perhaps, the specific incarnation of this view that Apple uses for the iPhone's home page may be customized.
Instances of generic UIView views that you might touch-and-drag will receive touch events. By overriding methods in the view, these events can be processed and passed to the page control, in order to tell it to "sweep" between pages.
If I wanted to do what you're asking about, that's how I might approach it. It seems doable to me, in any case.
Start with this: Swip from one view to the next view
Try using a UIButton that tracks the time since the state of the button changed to "highlighted". You may need to do this in order to track the dragging and move the button around:
Observing pinch multi-touch gestures in a UITableView
Check to see if the button starts overlapping one side of the screen while being dragged. If s certain amount of time elapses since the button first started overlapping the edge and then manipulate the UIScrollView so that it switches to the next page on the corresponding side of the screen
You may need to use NSTimer to keep track of how long the button is held down, etc.
In any case there's no reason why this couldn't work.
If UIButton doesn't work then perhaps try a custom subclass of UIControl (which tracks the same touch down actions etc.). If that doesn't work then use the window event intercept thing to track everything.

What to replace a UIButton to improve frame rate?

I created a minesweeper clone game and I made a 30 x 30 grid of cells. Now in my initialize design, I planned to use UIButton for each cell so that I can use its touchDown and touchUpInside. But my problem is using UIButton makes the game slow in zooming and especially in loading or adding each cell(900 pcs) in a UIView.
Now I'm planning to use images instead of UIButton and I heard about CALayer for easy animation. Can you suggest how can I use this CALayer for my implementation or replacement of UIButton?
Thanks.
I'd certainly suggest going down the Core Animation route for this. You can use an NSView subclass to recieve and handle the touch events then you could then use the location of the touch to calculate the cell that's been hit and update your model appropriately.
You arrange CALayers in a heirarchy so - whilst the complexity of the tree of views will depend on other UI elements you wish to have - you'll have a CALayer containing the 900 sub layers.
Using Core Animation will also allow you to get wizzy with animations.
It's going to be a bit more complex, but you just need to bite the bullet and get stuck into the Core Animation documentation and/or buy a book.
Good luck.
Edit: Ivan suggests using the hitTest message. This way you can get the layers to tell you which one of them was hit. Using that is obviously quite nice. However if you're looking for speed it might be quicker to avoid that and just work it out. This does make assumptions about how your game works (i.e. the buttons/cells don't move location). If you get the chance try both and let us know how it works out. :o)
On Cocoa desktop, there's NSCell, which is a lightweight alternative to NSButton, but that's an older technology before CALayer and Core Animation (they don't conflicts with each other)
To make custom button, create a custom class either extends to delegates CALayer. Then to know which button is clicked, use [hitTest] method which will return the layer being clicked efficiently.

Architecting a visual grid of 'buttons'

I'm trying to design how to implement a relatively simple application.
I want to create a grid of 'squares' that cover an entire screen. Each square should have the ability to respond to touch events, and then have an on/off state. For example, if I touch an 'off' square, and then drag my finger across 10 other squares, I want them all to turn on. And vice versa.
I'm not sure of the memory overhead of just creating a grid of 150 buttons. Also buttons don't have a settable state, from what I can see. I was also thinking of subclassing UIView and implementing UIResponder methods. It feels like I should be creating an array of array of buttons (or subclass of UIViews), but I'm not sure if that's possible.
I'm assuming that I can tell what square I'm on by getting the location of the touchevent from the UIResponder methods. Do I need to create my own version of a myButton by subclassing UIView, and have a on/off state property, along with UIResponder methods, and then create an array of myButtons?
UISwitch is the only thing that does this at the moment, though some have had good experiences using the UISegmentedControl for this as well.
Beyond that, you'll have to change the style/color of a regular button or image in code, which is what a lot of application developers do so it looks and reacts exactly the way they want it to.
Unless you need more of UIView's event handling stuff, you'll get the best performance if you use a single view and give it a -touchesBegan:withEvent, -touchesMoved, and -touchesEnded methods. Then use a custom drawRect method to draw your individual squares in either on or off states. You could also use layers, but trying to lay out 150 views is asking for trouble.