Skip to main content

Posts

Showing posts from 2014

Most Wanted (2014): Popular posts on the sketchyTech blog

By Jacob Spinks from Northamptonshire, England (starling fledgling) [ CC BY 2.0 ], via Wikimedia Commons Over 150 blogposts were released into the wild on the sketchyTech site this year. It has been a busy time to say the least. Here's how the most successful fledglings faired. Note: The number in parentheses is the number of page views recorded by Blogger for the post. Any post with more than 1,000 page views is recorded here, but this of course gives bias to earlier months, because more recent posts have had less time to accumulate views. January Enable your iOS app to share files via iTunes and to open files sent from other apps ('Open In') (1,877) February JSON and the JavaScriptCore Framework (Xcode/iOS) (1,559) Collections: Dictionaries, Arrays, Lists and Maps in iOS, Windows Store apps and Android (1,515) Changing the background colour in iOS (Xcode), Windows Store apps (Visual Studio) and Android (Eclipse)  (1,241) Important differences betwee

Dangerous Dungeons: Play the Game

I really enjoyed playing Duke Dashington by Jussi Simpanen when it was released earlier in the year, so I thought why not embed Dangerous Dungeons , an earlier incarnation of the game here for those with a few festive minutes to enjoy. The game runs using Stencyl and relies on the presence of the dreaded Flash. If you have Flash installed (or think you do) and are viewing this on a notebook or desktop then you are very welcome to play, simply select the appropriate button: Hide Game Show Game If you then want to hide the game again, simply press the hide button. See also Tightrope Theatre if you have the time. If you have any problems viewing or playing go direct to the game using the above link. (It's Z to jump and arrow keys for direction.) Follow @sketchytech

Swift: Failing with Class (failable initializer, nil coalescing operator)

The basics A failable initializer can exist in a struct, enum or class, but there are some special considerations when working with a class. So let's begin with the simplest form in which a class with a failable initializer can be written: class Fail { init?() { return nil } } It's important to note here that there's nothing to stop us using a force unwrapped init init!() in place of init?() But there's not a great deal to report about this fact beyond what is written here . What does it mean to fail? So what happens when a failable initializer fails? Adding in a deinit method class Fail { init?() { return nil } deinit { "Deinitialized" } } demonstrates that deinitialization still takes place, which leads us to the conclusion that initialization, albeit incomplete, takes place as well. After all that is the point isn't it? Full instantiation cannot take place because some important piece of the puzzle

Swift: Blogs of Note 2014

It's been a busy year for the new-born Swift, so I thought it about time to identify some of the bloggers and aggregators actively supporting this fledging language. Season's Greetings! (Picture from Wikimedia Commons .) Airspeed Velocity Always the first on the scene with new findings, t his blog lives up to its name. AlBlue's Blog Quality posts, hoping to see more of them in the New Year. Apple Swift Blog Apple's official blog. Wayne Bishop Unafraid to face up to the tough tasks and to explain them to the rest of us. Cocoaphony It seems there is no question that Rob Napier cannot answer on StackOverflow , and the same quality shines through on his blog. Coding Explorer An extensive range of articles on an endless number of useful topics. Coding in Swift  (Aggregator) An aggregator of note, and big on Twitter. Command Shift A busy guy with lots of projects. He shares when he can. Nate Cook This blog gets stuck right into the code an

Swift: The quiet affair of the failable initializer and the nil coalescing operator

It seems strange to me that no mention of "failable" initializers is made on the page of the Swift documentation detailing the nil coalescing operator , and that equally there is nothing about nil coalescing on the failable initializer page . Nor does the Apple blog post that introduced failable initializers link them with the nil coalescing operator. And yet, the nil coalescing operator makes working with failable initializers a simple affair and appears well matched to classes like UIFont, where should a bespoke request fail it is possible to have the fallback of a system font delivered through a type method. For example consider this: let font = UIFont(name: "Georgia", size: 18.0) ?? UIFont.systemFontOfSize(18.0) where the initializer can fail if the font named does not exist but the type (or class) method guarantees that a font will be returned. It seems a match made in heaven and results not in an optional or implicitly unwrapped instance but a straight

Review: QuartzCode (Mac App Store)

QuartzCode is a newly released app that enables you to draw and create CABasicAnimations and CAKeyframeAnimations using a GUI that incorporates a drag and drop timeline interface. The animation is then exported in Swift or Objective-C ready to be placed inside an iOS or OS X app. It's a nifty idea that could be a godsend for app studios on a low budget in need of enabling animators to supply code direct to programmers. Real-world use I attempted in the limited time I've had with this app to try a bit of everything. And there were areas that I immediately got to grips with and others where I admit that I needed a bit of a think or to refer to the online help . The inspector panel is very easy to work with. Shapes can be drawn quickly and I very much like the way in which the drawing of arcs is handled. Also good is the way in which the app uses property names so that there is a direct correspondence between the terminology of Xcode and the terminology of the app but

Swift: Let's talk about text (UILabel, CATextLayer, UIView, CoreText, UITextView, UIWebView)

In preparation for a series of posts looking at the presentation of text in iOS, this post describes use cases and an overview of the main options. There are a number of posts based around text display on this site already, some of which are referenced in this post, but these are mostly older posts written with Objective-C code examples, or only discuss text as part of a larger project. This is something I hope to rectify with the posts that follow this one, but first let's get to know the main options for text display. UILabel When programming for iOS, the entry point for most people into the world of text is the UILabel. The UILabel can be conveniently dragged and dropped onto a view inside a Storyboard file and does many of the things that are needed for the display of a small amount of text. The UILabel class is extremely flexible once tamed. It is capable of displaying text across single or multiple lines, text can be wrapped and/or truncated, as well as being style

Swift: Notes on the relative positioning and transformation of layers and sublayers (CALayer, frame, bounds, transform, position, Xcode)

When a CALayer is created using CALayer() it is constructed with its name as nil, and its frame values, bounds values and position values set to zero. let myLayer = CALayer() self.view.layer.addSublayer(myLayer) And this remains true after an unsized frame is added to a layer as a sublayer. Sizing and positioning a layer frame myLayer.frame = CGRect(x:CGRectGetMinX(self.view.bounds),y:CGRectGetMinY(self.view.bounds),width: 200,height: 200) Once frame values are set, the bounds and position properties also change relative to this. The height, width and position of the frame and bounds are at this stage identical and the position x,y values are half the width and half the height respectively (because the layer is by default positioned on its centre, that is its "anchorPoint"). Here are a list of the set values based on the above code: name = nil Frame X: 0.0 Frame Y: 0.0 Frame Height: 200.0 Frame Width: 200.0 Bounds X: 0.0 Bounds Y: 0.0 Bounds Height: 200.0 Bounds

The Inspiration of Hexagons for Drawing in 3D

Hexagons have become a fascination (read obsession) for me over the last few weeks and I'm constantly looking for books either from a fine art angle, a mathematics angle or a computer science and three-dimensional drawing angle that discuss the connection of hexagons to 3D space. I keep finding glimpses, stumbling over new names and books. For example,  Buckminster Fuller , who happened to invent the geodesic dome ( see also ). Friedrich Kittler , and a whole host of other material that leads into areas, some of which I'm yet to grasp and others that I'll probably never grasp. The next thing that I've started pondering is hexagons and optical illusions. There must be a book out there somewhere, right? Well, it seems the world of quilting  shares as much knowledge about how hexagons relate to the optical illusion of blocks  as anyone. And provides yet another tangent to this hexagonal pondering. Dreaming of the one book What seems clear is that recorded knowledg

The Power of Hexagons: Cube in a Spin with Swift

In an earlier post , rotation was considered and here I consider spinning a cube. In order to achieve a clockwise spin: index 0 follows the path of the hexagon's surrounding radius to index 5 index 1 follows the path of the hexagon's surrounding radius to the index 0 origin follows the path of a circle (centred on index 0) to the index 1 index 2 follows the path of a circle (centred on index 3) to the (rear) origin index 3 follows the path of the hexagon's surrounding radius to index 2 index 4 follows the path of the hexagon's surrounding radius to index 3 rear origin follows the path of a circle (centred on index 3) to index 4 index 5 follows the path of a circle (centred on index 0) to the origin The rotation happens for a distance of 60 degrees and then repeats. The colours fading between one another appropriately to reproduce a very basic imitation of light shining on the surfaces. The code leverages the CAKeyframeAnimation class to stitch th

Messing with the Matrix: CGAffineTransform (Swift, iOS, Xcode)

I've been writing quite a few posts recently about CGAffineTransform and CATransform3D functions. They are something that I never really took the time to grasp until recently and wanted to share some findings, mainly because reading around it appears I'm not the only one who has failed to immediately understand the logic of these transformations. Apologies for any repetition or cross-over with earlier posts, but there is quite a bit of new information in this collection of notes. Direct manipulation of the matrix The following transformation makes no changes to a UIView that it transforms: CGAffineTransformMake(1, 0, 0, 1, 0, 0) It simply multiplies x by 1 and y by 1 and repositions each point using this formula. The result of which is that x and y points remain static. It does not change the default, or "identity transform", and so simply repeats the values of each matrix position that you would find should you work through the transform value of any (untrans

Explaining the CATransform3D Matrix: Translation, Scale and Rotation (Swift, iOS, Xcode)

To understand the basics of using CATransform3D functions, see this earlier post and this earlier post . The current post explains in greater detail what is meant by the matrixes presented in the Apple documentation under the details of CATransform3D functions and it is advisable to read first this post on CGAffineTransform matrixes in order to fully understand what a transformation matrix is. Translation CATransform3DMakeTranslation()  tells us that it Returns a transform that translates by '(tx, ty, tz)'. t' = [1 0 0 0; 0 1 0 0; 0 0 1 0; tx ty tz 1] As with the CGAffineTransform matrix , this should be read as a grid top to bottom: 1  0  0  0 0  1  0  0 0  0  1  0 tx ty tz 1 Where the translated x position should be read: x'  = 1 * x + 0 * y + 0 * z + tx The translated y position should similarly be read: y'  = 0 * x + 1 * y + 0 * z + ty The translated z position: z'  = 0 * x + 0 * y + 1 * z + tz And the 1 (factor): 1 =  0 * x 

Explaining the CGAffineTransform Matrix (Swift, Xcode, iOS)

Introduction Wherever you see a CGAffineTransform or a CATransform3D, Apple supplies a matrix illustrating the transformation. For those who understand matrixes this is great, but for the rest of us it's a WTF moment. The aim here is to walk through the interpretation of these matrixes by adding small but significant pieces to the puzzle for those who collapse on the floor when faced with these illustrations. This post deals with CGAffineTransforms but I will examine CATransform3D matrixes in a later post. Explaining the matrix Let's look first at this translation matrix from Wikipedia : Taking the first row of the matrix it can be read as: x' = 1 * x + 0 * y + t x We arrive at this by treating the first numeral as a number by which x is multiplied and the second numeral in the row as the number by which y is multiplied, while t x  is whatever number is passed as the translation. And added together together they equal x' or in other words the new (tra