Core graphics and UIKit interaction - uikit

Hey all, I'm am trying to make a very interactive UI with lots of animations and effects.
But I don't know if:
Core graphics can support user interaction (touches, drags, etc)
Core graphics supports object rotation
Core graphics can interact with UIKit and Core Animation in any way
Thanks!

Assuming you are talking about iPhone not Mac (because of mention of touches).
1) CoreGraphics is mostly associated with drawing of images. User interaction is via your view and the touches* member functions. Within the view's drawRect function, you can use CoreGraphics to do custom drawing.
2) Yes, you can get rotation, but easiest way is to set the transform property using a CGAffineTransformMakeRotation. Drop down to the layer and you can even use 3d transforms (which is I think how they do stuff like cover flow).
3) See #1.

Related

Frame-by-frame animation in MacOs

I'm looking for a way to do frame-by-frame programmatically drawn animations in a MacOs application (not keyframe property animation). I have tried drawing to CALayers using the drawLayer:inContext: delegate method, calling setNeedsDisplay to draw each frame, however I'm getting poor performance doing it this way. Is there a recommended way to do this type of animation in Cocoa?
A good way to do entirely custom animations is by using CADisplayLink (iOS) or CVDisplayLink (macOS). CVDisplayLink is basically a timer that fires as often as the display refreshes.
You can then calculate your own timing functions based on the values you get off CVDisplayLink. The API is still C so it is a bit cumbersome to use, especially in Swift, but once you get how it functions it works like a charm.
I have only had good experiences with CVDisplayLink, especially with layers. They are really performant. I was able to animate 1000+ layers CVDisplayLink driven at 60fps without any problems.
If you need any help in using the API, feel free to ask!
Alternative:
If you want to use a more modern API, I can recommend SpriteKit. There are some nice animation APIs as well. And they perform really good. Apple uses it itself to draw more complex views (like the Memory Debugger in Xcode).

What type of canvas is used by SketchApp, PaintCode or Monodraw - Cocoa

I am wondering what type of drawing canvas is used by SketchApp, PaintCode or Monodraw ...
Image View, OpenGL View, a Custom View ?
I like the fact that we can zoom, translate and select object in this canvas (but I guess it's handmade features).
So, what do you think is the best way to achieve this in Cocoa ?
As for PaintCode, we use NSScrollView with OpenGL view inside for custom multithreaded tiling. The actual content is drawn using CoreGraphics, so what you see in PaintCode is what you get in your app.
I directly asked Monodraw developers and they told me :
The grid is an NSScrollView with custom rulers.
:)
You can check their ASCII art editor here : https://monodraw.helftone.com/

Cocoa control to render a tile map

Is there any Cocoa control that is capable of drawing tile maps with multiple layers and multiple texture sources which can also intercept touches into single tiles? Having multiple layer support is not a real requirement but an optional feature (the views could still be stacked). Hardware acceleration is not needed at all.
So far I have toyed around with NSMatrix, IKImageBrowser and NSCollectionView but non of them felt like a good solution for the problem. Ideally I need an control similar to the one in Tiled.app. Is there anything, third party or built-in, or do I have to handcraft this control?
I fear that you will be hardly able to find a ready-to-use control for managing tile maps.
If I had to implement something like that on my own, I would consider using CATiledLayer, since this is the closest thing to a tile map control that I know of.
From CATiledLayer Reference:
CATiledLayer is a subclass of CALayer providing a way to asynchronously provide tiles of the layer's content, potentially cached at multiple levels of detail.
There is a nice sample by Bill Dudney (the author of "Core Animation for MacOS and the iPhone"). This sample could provide you a solid foundation for your own project, though it only displays one single PDF, allowing you to zoom in the area you clicked on (this requires rereading the tile at a different detail level).
Another interesting introduction can be found here. This is more step-by-step, so you might start with this.
Finally, on Cocoa is my Girlfriend there is a nice article, although it focuses on iOS, but you may find it anyway relevant.
Cocos2D supports building mac applications now
Article on cocos2d stating this: http://www.cocos2d-iphone.org/archives/1444
Aee here for how to do it: http://chris-fletcher.com/tag/cocos2d-os-x/
Aee here on how to use TMX tile maps with Cocos2D to build tile based maps: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps
This means you can use the power of Cocos2d and you will have to write much less code to get to where you want with a tile based map.
If you don't want to use Cocos2D:
It seems you would have to code it yourself, but it shouldn't be too hard to do.
First you can create your .TMX file using the tile editor "Tiled.app" then you would need to parse the XML using a standard xml library for Cocoa.
To lay out the tiles use a UIView for the overall container and then create a tile class that holds your tile display information and responds to clicks the tile class should extend UIView. For the tile class allow the assigning of a click delegate and assign your ViewController as the click delegate for all tiles so you can handle clicks easily with the clicked tile being passed to you.
Loop through your xml data and create and position the tiles in the overall UIView by using the tiles width/height and your tilemaps rows/columns.
I think in about a day or 2 you could have the tile map being rendered and clickable using the standard TMX format which will allow you to edit your map in "Tiled.app"
The TMX standard is covered here: https://github.com/bjorn/tiled/wiki/TMX-Map-Format
route-me might fit the bill.

Core Animation architecture

From the documentation it appears that core animation layer is above OpenGL and Quartz2D. i.e.
executing a core animation command should produce a sequence of Quartz2D and OpenGL commands Am I right?
In interface builder, under the View Effects tab, we can set the core animation layer. What happens internally there?When we tick Context View option, contents on screen (buttons, scrolls etc) are not drawn using main context or currentContext(view), but new Bitmap Context is created for them. What is happening under the hood there?
Can somebody please explain me relationship between CoreAnimation Layer and Quartz2d/OpenGL?
Core Animation layers are essentially high-level abstractions of OpenGL surfaces. They are stored and manipulated by the GPU and so manipulation of the layers is extremely fast. CALayer objects by themselves are very lightweight and have no event handling.
Layer-backed NSView objects (which is what you get if you enable the checkboxes in Interface Builder) are views that draw their content into a Core Animation layer, again stored in the GPU's memory and with the same performance advantages as plain CALayer objects, but with all the functionality of a normal NSView.
What happens is that the view's content is rendered (via Quartz) to its backing layer (essentially an OpenGL texture). The view then only needs to draw again if the content of the layer changes.
Changes in position, scale, rotation etc of the view's layer do not require the view's content be redrawn. This means that most of the time the CPU does not have to get involved in constantly redrawing the view.

Cocoa GUI - effects, polish

I'm creating an in-car control screen (will be run from a Mac Mini) and am looking for some libraries or code samples for "effects". For example, I might want the name of the current track playing to fly in from the right. I might want screens to fade or slide up, etc.
I am aware that I can manually write these effects in Objective-C.
I am hoping there is a library like scriptaculous for JavaScript that allows me to easily manipulate an existing TextView, ImageView, etc.
A framework or otherwise is preferred. I'm working in native cocoa. I don't mind if the library costs $.
Thanks,
Rick
Have a look at Core Image and Core Animation, both of which will allow you to add visual effects. Core Image, as its name implies, works with images only but can do fancy transitions. You can "fake" UI animations with it though by rendering a view to an image, swapping the image in in place or over the top of the view and then running a transition to another view.
Core Animation works directly with Cocoa Views and does have some transitions available. Both APIs (especially Core Animation) are fairly complex and have a learning curve.

Resources