Category Archives: ActionScript

Event Dispatching in Swift with Protocol Extensions


via Event Dispatching in Swift with Protocol Extensions.

One of the highlights for Swift 2.0 at WWDC was the introduction of protocol extensions: the ability to add default method implementations to protocols. Plenty has been written about protocol oriented programming in Swift since WWDC from bloggers such as SketchyTech, David Owens and Ray Wenderlich, and I thought it was high time to put my own spin on it.

After working with event dispatching in ActionScript for may years, protocol extensions seemed the perfect technique to implement a similar pattern in Swift. Indeed, protocol extensions offer the immediate advantage that I can add event dispatching to any type of object without the need for that object to extend a base class. For example, not only can user interface components dispatch events, but value objects and data structures can too: perfect for the MVVM pattern where a view may react to events on the view model to update itself.

My project, Protocol Extension Event Dispatcher, contains a demonstration application containing a handful of user interface components: a slider, a stepper, a label and a button. There’s a single ‘model’: an integer that dispatches a change event when its value changes via those components.  The end result is when the user interacts with any component, the entire user interface updates, via events, to reflect the change.

This isn’t meant to be a complete implementation of event dispatching in Swift, rather a demonstration of what’s possible in Swift with protocol oriented programming. For a more complete version, take a look at ActionSwift.

Getting started with UIKit Dynamics in Swift


via Omar Fouad – Getting started with UIKit Dynamics in Swift.

As you may already know, Swift, the new programming language from Apple, has been out for quite a while and almost every iOS developer is today having the itch to make the most out of it.

The first thing you’ll notice in the language is its similarity to most of the dot-notation based languages out there, like JavaScript, ActionScript, Java and so on. This is a massive advantage for people who didn’t have the guts to approach iOS development because of the complexity of Objective-C. Now everyone with a bit of programming experience is ready.

I won’t get into details about the language itself. If you’re new to the language I suggest to read the Swift Cheat Sheet, or check out Design+Code by Meng To, which I strongly recommend, especially for beginners. If you want to dig deeper, you can download the official E-Book from Apple, free of charge.

In this post I’m going to talk about UIKit Dynamics, a framework used to create complex 2D animations. While we could use the Core Animation framework to perform basic transitions, the new Dynamics framework includes real-world physics behaviors out of the box, like gravity, collision, density and so on. The UIKit Dynamics framework is also very easy to implement, which means you don’t have to be a particle physicist to get started.

So let’s start with a simple idea looking at the real world around us. Imagine you’re standing up holding a box with both your hands perpendicularly to your body. The box has a specific weight and it is made of a specific material. Also there is gravity that is trying to pull it down on the floor, so you’re applying some kind of resistance or force to hold it. If you let the box go, it falls with a certain speed until it hits the floor which is also preventing it to keep falling forever. When the box hits the ground, chances are it won’t just hit and stop. It might bounce around for a while based on it’s velocity, density or bounciness, or just crash into a million pieces.

This makes sense, but how do we simulate this from within an application? The image below shows the final demo.

Creating Custom Gesture Recognisers in Swift


via Creating Custom Gesture Recognisers in Swift.

You may have used gesture recognisers in your Swift projects already, for example UIPinchGestureRecognizer for handling pinch-to-zoom or UIRotationGestureRecognizer for detecting a two touch rotation. This post looks at creating bespoke gesture recognisers and illustrates the technique with a single touch rotation gesture. The companion project for this post is available at my GitHub repository here.
The original code for the single touch rotation gesture comes from an ActionScript project I did back in 2008. Although I originally thought it would be useful for games, it could also be useful as a jog/shuttle control for a video application.
The UIGestureRecognizer base class allows us to decouple the logic for recognising and acting upon user gestures. Rather than adding code directly to a view or controller, delegating this code to an extended UIGestureRecognizer allows for code reuse and supports the single responsibility principle.
The Swift code for implementing an existing gesture recogniser, for example UILongPressGestureRecognizer, in its simplest form looks like this: I create an instance of the recogniser, set its target to the component that I want to respond to the gesture and define the method to invoke in response to the gesture:

NodeJS: Inversion of Control


via Node.js: Inversion of Control — Medium.

I consider myself a polyglot developer. In my (6 year) career I’ve been paid to code C++, Python, PHP, ActionScript, Ruby, plus the whole menagerie of tools and languages that make up front-end web development. I’ve dabbled in many more unpaid. Picking up a new language is pretty straight forward, picking up the idioms of that language (and in-depth knowledge of the frameworks/standard library of that language) is much harder. I write Java like a PHP dev and JavaScript like a Pythonista.

In my most recent project we’ve taken the decision to break apart our monolithic app into microservices (partly to address some scaling concerns, and partly as a premature optimisation for some future features which would have been more challenging under our previous architecture), and we’ve decided to build those microservices in Node.js.

JavaScript is a famously unopinionated language, this makes life hard as a dev. As a Python dev I could follow PEP-8, as a Java dev I used Spring, which forces you into a certain style of design; JavaScript has many ways of doing things with no clear one that’s best. I’ve written before about how I structure my JavaScript using AMDs, but back-end code has different behaviours, different requirements, so I started writing my JS in an approach I felt was tried and tested, as classes using Inversion of Controlwhere the dependencies are passed into the constructor. My top-level JavaScript module behaved as the IoC container, instantiating objects and passing them around where they were needed. It made each module easy to test (I could just pass mocks into the constructor) although without a DI framework it did make that top-level module a bit messy.

Asynchronous GIF decoder written in ActionScript 3


Async GIF decoder

An asynchronous GIF decoder written in ActionScript 3 that lets you play animated GIFs in flash without freezing the UI.

Inspired by Lee Burrows’ Async-Image-Encoders, based on Thibault Imbert’s as3gif.

var gif:GIF = new GIF();
    gif.addEventListener(Event.COMPLETE, function(event:Event):void
    {
        trace("done", gif.totalFrames);
        gif.play();
    });
    gif.addEventListener(IOErrorEvent.IO_ERROR, function(event:Event):void
    {
        trace(event);
    });
    gif.load(new URLRequest("smile.gif"));

addChild(gif);

If you want to play GIFs in your starling based app, you can use GPUGIF class.