Different ways of animating between a series of images in Cocoa - objective-c

I have a series of image (PNG) files, about 50 of them. If displayed quickly one after another they form a smooth animation.
How can I create this animation in Cocoa, within an NSView? What method would you suggest, and how would you go about doing it?
Most answers here seem to suggest Core Animation, but I need it to be in an NSView which accepts mouse events and drops, so I'm not sure if this will work.

Core Animation is best way to display a smooth animation.
NSViewAnimation also can create a animation, but may not smooth as Core Animation.
NSView also can add Core Animation layer.
[NSView setWantLayer:YES]
In your question, You want accepts mouse events.So, I suggest to Use Core Animation to display animation at NSView,As NSView just accept events,and let layer to animation.
Sorry about my poor English,Hope I expression clearly. :)

Related

How to change from NSView subviews to CaLayers in a MacOs App?

I have a music macOS Application that displays a piano roll with note events in a midi file. I am representing them as bars in a view, using a main NSView and a lot (typically 5000/10000) of subviews.This performs quite well, the only problem is on intensive tasks, like when I zoom the view, or I change the proportions from the GUI, I believe all the views receive a setNeedsDisplay message and resize accordingly. I was thinking,
Could I switch my implementation to CALayers instead of subviews ? the big NSView works in a NSScrollview. Perhaps I could try to implement some sort of lazy updating where only the visible notes get updated.
Thanks for any help.

Too many UIViews causes lag

I am creating an app for practice that is a simple drawing app. The user drags his/her finger along the screen and it colors in a 100px x 100px square.
I currently achieve this by creating a new colored UIView where the user taps, and that is working. But, after a little time coloring in, there is substantial lag, which I believe is down to there being too many UIViews as a subview of the main view.
How can I, and others who similarly create UIViews on dragging a finger reduce the lag to none at all, no matter how many UIViews there are. I also think that perhaps this is an impossible task, so how else can someone like me color a cube of the size stated above in the main view on a finger dragged along the screen?
I know that this may seem like a specific question, but I believe that it could help others understand how to reduce lag if there are a very large amount of UIViews where a less performance reducing option is available.
One approach is to draw each square into an image and display that image, rather than keeping around an UIView for each square.
If your drawing is simple enough, though, you can use OpenGL to do this, which is much faster. You should look at Apple's GL Paint Sample Code which shows how to do this in OpenGL.
If your drawing is too complex for OpenGL, you could create, for example, a CGBitmapContext, and draw each square into that context when the user drags their finger. Whenever you draw a new square into that bitmap, you can turn the bitmap into an image (via CGBitmapConxtextCreateImage) and display that image an a UIImageView.
There are two things that come to my mind:
1- Use Instruments tool to check if you are leaking any memory
2- If you are just coloring the views than instead of creating images for each of them, either set the background color property of UIView or override the drawRect method to do custom drawing
I think what you are looking for is the drawRect: method of UIView. You could create your custom UIView (you propably have that already) and override the drawRect method and do your drawing there! You will have to save your drawings in an array or another container and call the setNeedsDisplay method whenever the array content is changed.

Stacking UIViews with blend modes in iOS

I have two different UIImageViews. I'd like to make the top UIImageView blend in using the Screen blend mode with the bottom UIImageView.
I know of the property of CALayer: compositingFilter and I know that it doesn't work in iOS. I've searched a lot for solutions, and I've found how one should subclass UIView and override drawRect.
I've tried to set the context in drawRect to the screen blend mode, although it still draws every one of the images normally. Perhaps I am doing something wrong, or the approach should be different. Maybe I need OpenGL or CALayer to achieve this. Could someone assist?
Unfortunately there is no way to do a non-composite blend between UIViews on iOS. UIKit doesn't provide the functionality, and you've already noted, CALayer can't do it either.
In general, implementing -drawRect in a UIView won't help you. You're drawing into an empty bitmap -- it doesn't contain the bits of the views behind it, since those might change at any time (any view or layer might be animated). CA fundamentally assumes that layers' contents should be independent of each other.
You could try, in your -drawRect:
create an image context
capture the views under your view using -[CALayer renderInContext:] for each
create an image from the image context
draw that image into your view
set the blend mode and draw on top of that
But that will be slow and fragile, and won't work if you animate any of the views. I wouldn't recommend it.
If you really need to do this, you're going to have to switch your whole scene to render with OpenGL, where you've got more freedom.

Replicating iOS 5 Stocks Widget

Anyone have any ideas on how I could go about replicating the scrolling stocks ticker in Notification Center on iOS 5?
What I'm thinking is using a UIScrollView, and use a UIView subclass for the items in the ticker. Would using an NSTimer be the best way to have it scroll automatically? How would I handle manual scrolling acceleration and deceleration like the Apple stocks widget does?
Also, how do I handle infinite scrolling?
Thanks for any suggestions.
Try AutoScrollLabel. It's a relatively old class, but the code design and animation behind it may be useful. If I understand correctly, it's a UIScrollView that holds two labels, each saying the same thing, resulting in "scrolling" as it animates between them. I'm sure you could take that concept and the drawRect: method in the code to create your own subclass of the UIScrollView and animate whatever content you want, complete with infinite scrolling. There may very well be an easier way, but I've used AutoScrollLabel in a few apps and it's easy to use and works without a problem. Let me know if you need more info/a usage example. Hope this helps!

Best approach for drawing a graph of almost infinite size in a UIScrollView on iPad

I'm currently working on an app which needs to draw s.th. like a network graph. Unfortunately this graph can become very big with thousands of movable objects.
I tried to put a giant UIView inside a UIScrollView but soon noticed this won't work because of memory limitations.
So I tried another approach: currently I have a UIView which has exactly the size of the visible part of the UIScrollView. The scrollview is set to not handle the scrolling (only the pinching). Instead I handle the scrolling in the UIView. Everytime a user scrolls, all graphic objects (those graphic objects are currently just subclasses of NSObject, which contain custom drawing code) are moved, so it seems like the view is scrolling. In the drawRect I only draw the graphics that are currently visible.
Also I constantly add and remove sublayers if they are moved out/in the visible frame
This works very smooth even with thousands of objects.
Unfortunately this approach has some drawbacks:
I can't zoom out to see all the objects in the graph, instead the user can only see a part of it
I don't get the inertial scrolling the UIScrollView offers
Other approaches I tried, like the CATiledLayer, don't work either because all the objects in the graph are draggable by the user and it looks really ugly if I use a CATiledLayer...
Swapping out the UIView with other UIViews while the user scrolls may help with the inertial scrolling, but it makes everything more complicated and zooming out completely still won't work :-(
Do you know of any best practices to draw graphs that can be very big?
//edit: I ended up with a uiscrollview which has a subview which has a cascrolllayer which has many sublayers. While zooming in and out the frame of the uiscrollviews subview is constantly changed to the uiscrollviews bounds by view.frame = scrollview.bounds. While dragging the scrollview the cascrolllayer is always forced to scroll to the current offset of the scrollview.
I needed to subclass the uiscrollview and hack around in order to make the zooming work nicely, but it's working well now. This approach works very well and allows very big graphs with lots of draggable elements.
//edit: see my other answer below, the approach above didn't work out as well as I initially thought, especially the zooming part
CATiledLayer is definitely what you should use here—there’s not really another solution that’ll let you use Quartz/UIKit drawing on a huge zoomable canvas. For anything that needs to be interactive (dragged or animated or whatever), you can disable its display in the main tiled layer and overlay another view or layer on top of it that just contains the object being interacted with.