MVC - Game - Objective C - iphone

I am developing a game for the iPhone. The game basically animates objects on the screen, allowing a user to click on them. I am trying to use the MVC pattern. I have the functionality laid out as such:
Model:
Holds data about the targets (speed, relative size, image, etc)
Has timer running that adds targets to the list (should this be in the controller?)
Controller:
Subscribes to events fired from the model (such as target added)
Subscribes to events fired from the view (such as target clicked)
View:
Displays targets
The flow can be as follows:
Controller tells Model to start game
Model fires timer which says to add a target
Controller hears event and passes it to view
View adds image to screen (animating it)
User clicks on image
View fires event which says image was clicked
Controller hears event and passes it to model
Model removes target from itself
Lastly, I am unsure where to put the animation. Should the view construct the animation (based off of settings from the model)?

I'd consider having the view or controller handle the animation so you can take advantage of Cocoa's built-in animation support. For example it would go like this:
View queries model for position
View adds image to screen at position
View queries model for velocity
View computes new position after 0.1s and animates image to new position
Cocoa fires animation complete event
View hears event
View computes new position after 0.1s more and animates image to new position
And if targets can change velocity, you have some options. You can have the model fire events for velocity changes, and cancel the animation in progress and start a new one. Or you can just have the view requery the model each time an animation completes, ask for the valid position and velocity, and compute the next position. With the latter there would be some missynchronization between the model and the view, but with updates every 0.1s it wouldn't get too far off. It depends how precise you need to be.

I would think that an animation would be part of a model (or a model within a model), specified by a controller, and rendered by a view.
Here's a good example for the iPhone:
http://www.bit-101.com/blog/?p=1969

Related

UIImageView dragging jerky when used in Screencapture view

I am working in a project where I need to drag the images and create the video for the same,
it works fine when I use it in the main view.
I use ScreencaptureView.h and ScreencaptureView.m file for video recording, I subview it in my main view and add subview the image object in the capture view.
Now the problem is because I subview the image in the ScreencaptureView the dragging becomes slow.
What could be done to solve this issue.
I'm not surprise that it slows down your animation. The amount of data to capture and save to memory as images is huge. No miracle here. The data have to be flushed from your graphic memory to the RAM and then to disk and that takes a long time. What I recommend to you is to do it in 2 steps. The main idea is to capture the less data as you can when you are in real time.
- step 1 : CAPTURE IN REAL TIME
Don't put your subviews in ScreencaptureView for this step but in a normal view or disable the capture if the option is available.
Capture only the translation values you make on each image in a dictionary for example where the keys refers to your images (NSValue with pointer to your image). Save one dictionary per frame. Then you have an array of dictionaries. You can imagine other solutions to store your dumped datas.
You can do it easily at 30 fps or 60 fps.
That way your capture is very lightweight and the animation will be totally smooth for the user.
-step 2 : REPLAY - POST-PROCESSING
Now you can put your subviews in ScreencaptureView or if already simply activate the capture mode.
Replay your animation by reading you recorded values and re-apply the corresponding translations to the images (you have to keep them of course) and capture the frames as usual. Now it can be slower but it does not matter. The frame will be processed one by one.
This is done ! You have now a smooth video screenshot of your animation.
Is it clear ?
try this
during touch began event, first, add the view being dragged to root view as subview. call
bringSubviewToFront method to keep the dragged view top of the root view hierarchy.
Make sure the root view should be a parent for both dragged view and
target view(ScreencaptureView in your case).
during touch end event add the dragged view to the target view(ScreencaptureView).

Events cannot be dragged to a new day. This should be possible by, say, holding it over the side of the screen for a few seconds

I am working on Events functionality in Calendar and I got stuck on one thing.
Here is what I have done:
I have a page/interface in which day view is visible. User can scroll to next/previous day and same view is reloaded with the specific day information. All these things are done dynamically. There are events for specific day displayed on the day view.
Issue:
I want to implement the move/ Drag and Drop functionality for the event displayed. User can pick the event block and move it to another day. However I am not able to implement the same. I have attached the screenshot of the Day View in calendar with the Event visible.
Please help me move the events with event object to different days (Like Iphone4 calendar ). How can I do this?
I assume you can't achieve the desired effect because you track movement on the Event View or the Day View. When you then load the new Day View, your gesture recognizer loses its context. If this is your problem then the following steps could help:
Identify a view which is a superview to the Day View and does not unload when another Day View is loaded.
Add your UITapGestureRecognizer which tracks the Event View movement to the controller of that view.
Track the gesture position when the recognizer sends events in the state recognizerUIGestureRecognizerStateChanged and move the Event View as before.
When the position of the gesture is at your desired target area for a Day View change, put the Event view into a new subview of the superview of the Day View, load the other Day View and keep the Event View in the foreground (and put your Event Data object into the context of the new day).
When the new Day View has loaded, revert to whatever method you have used to move your Event View, now in the context of the new Day View.
Based on your sketchy question and my assumptions, this is the best answer I can give. Hope this helps.
Best, Peter
A workaround would be
long press on existing event
create a UIButton matching exactly as "existing event" and add to subview
make that UIButton draggable and with a target action
user scroll or swipe on background calendar (change date within same view, ie: your existing design)
user tap on UIButton, use position of button to find x,y offset along with current date.
Good luck!

Can I use Threading for dragging and UIButton event simultaneously?

I am having a customize user interface for my iPad Application, and There is a dragging of an Object and button event simultaneously. My Problem is that when i drag the object the animation start working and when i leave the object the animation still working for few time. In the mean time while i am trying to press button or dragging another object, its not working till the animation completes.
If you want to interact with the UIButton that's animating:
Using (core) animation; you can't interact with the elements during their animation cycle (the regular way).
Only when the animation is finished you can interact with them again.
You can make your own touch handlers on the main view which checks if the touch is 'nearby' the animated button.
If you want to interact with the 'rest':
Make sure you animate your object using the UIViewAnimationOptionAllowUserInteraction parameter:
During an animation, user interactions are temporarily disabled for the views being animated. (Prior to iOS 5, user interactions are disabled for the entire application.) If you want users to be able to interact with the views, include the UIViewAnimationOptionAllowUserInteraction constant in the options parameter.

How to designed a correct MVC on iOS?

I have a project where I want to display overlays to locate people on a map. I have difficulties to follow the MVC pattern since I've never practice it.
In a database, I'll save a position (in map coordinates) and the image to identify one person (the image of the overlay).
If 2 or more are at the same position, they are grouped under another "group" overlay. When an overlay is touched, I want to display different information, depending of its type ("group" or "single").
Here is my problem : I obviously need to subclass an Overlay superclass, which have the properties position and image, to redefined the overlayTouched: method. But this code his typically a View code of the MVC pattern, but the overlay is a Model object since it will be saved, so i'm confused.
How should I designed this ?
You should not subclass Overlay to add touch events to it, instead create a new UIView, YourNewView
This YourNewView will "has a" instance of Overlay, and it will delegate all its method that are required from the model to Overlay
For example. if you have a touchesEnded in your YourNewView and you want it to save the overlay to the DB, you would do
//in touchesEnded
[self.overlay saveToDb];
And so on, all the calls will be forwarded to the Overlay Class

How to implement view sliding in and out in iPhone?

I would like to implement the effect of a view sliding in, much like the animation of a view being brought in by presentModalViewController, but the view only slides in to cover half the screen.
I approached it this way:
1) Right before thew view should appear and slide in, addSubview the child view. Position it in such a way it's out of view in the beginning.
2) Call a method on the view controller of the subview to perform the Core Animation code to bring it in.
Doing the above didn't seem to work (nothing happens - the view just appears at its starting location). Adding the view ahead of time in viewDidLoad won't work either. Ideally, I would like the code to have the same requirement as presentModalViewController - which requires you to instantiate the view controller at the point you need to bring the view in and animate it.
Would appreciate if you can provide pointer or code on animating the view in/out as well.
I would check out the sample project ViewTransitions on the Apple dev site. It is a great resource for understanding the simple things you need to do when setting up transitions. If you wanted to only slide in half-way you could try setting the frame of the "sliding view" to only half the screen... just check out the code and give it a try.
I usually animate modal screen in the parent viewController instead of modal viewController itself.
Maybe you should post the code you use in step number 2.
I did something similar on one of my apps, where the user slides a view from one edge of the screen to the other as if placeing a cover over the screen.
If what you are trying to do is an automatic animation (without user interaction) I believe that you need to: set to the sliding view a frame representing the final position. Do this within an animation block. Play with the duration, animation curve, etc parameters.