Tag Archives: Popular posts

CATransition animation

CATransistion is very useful for animating data reload e.g. when you have details view with an array of items and you want to switch among them with some animation. You might not want to animate entire screen, you may animate layer of any view you want, for example you have a screen with some content and an image view in which you want to switch images periodically, with animation. You may add CATransition animation to the layer of the image view, and add all kinds of transition animation effects.

Here is a simple example for presenting previous/next item on a view with a simple push animation. You may add swipe left/right gestures to that view and assign the methods below to get the sliding effect.

Drawing shapes in UIView’s layer

Simple example on drawing lines that form a white border on a view’s layer. Link your binary with QuarzCore.framework and CoreGraphic.framework libraries and #import <QuartzCore/QuartzCore.h>

Graph theory – Breadth-first search example

I’ve made an example where graph theory and Breadth-first search comes in hand using a simple game. “Wordz” was very popular for a while, if you don’t know the game itself you are probably familiar with the game concept where you have to connect given letters which would then form a word. The more letters in the word, the more points you get. The game in this example has 4×4 letter matrix and the words may be assembled from letters in all directions (not just vertically and horizontally). For 16 given letters that form the 4×4 matrix, all possible words are listed (with matrix coordinates for connecting each letter in the word) using the Breadth-first search algorithm.

You can find the source on my GitHub repo:
https://github.com/lukagabric/WordFinder

There are three main objects: graph node, directional graph and the search controller.

Continue reading Graph theory – Breadth-first search example

Cocos2d – moving a sprite towards another sprite or a point

As I was working on a game in Cocos2d, a part of the game plan was that a sprite was chasing another sprite moved by the user. I implemented the method below in the “chaser” sprite so it would move towards the user’s sprite position which is passed as the point parameter.

Distance from point to line segment

I was recently working on a project where I needed to find the distance from CGPoint to line segment defined by two CGPoints. I used it for selecting drawn objects.

There are three cases:

If point A is closest – returning distance from point P to point A:

If point B is closest – returning distance from point P to point B:

Let’s say line X is defined by points A and B and line Y is perpendicular to line X and defined by point P making point Z the intersection of line X and Y – returning distance from point P to point Z. This is the only case where line Y intersects with line segment defined by A and B – previous two cases do not contain point of intersection so in those cases distances from point P to points A or B are calculated.

CAShapeLayer example – Round corners view with dashed line border

Drawing on a UIView is explained using a simple example of a view with round corners and a dashed line border. CGMutablePathRef is created and different kind of shapes are added like lines (CGPathAddLineToPoint) and arcs (CGPathAddArc). After you have drawn the path you wanted it is set as the CAShapeLayer object’s path. The CAShapeLayer object is added as a sublayer of a UIView. Now you have the graphics on the UIView. This way a border may be drawn around a UIView, as in this simple example.

Continue reading CAShapeLayer example – Round corners view with dashed line border