Swift: Put overlay on top of AVCaptureVideoPreviewLayer

With help of AVCaptureVideoPreviewLayer you can output video stream from camera on the phone’s screen, here’s how to do it without video or audio recording, just to capture the video and output on the main view

Continue reading “Swift: Put overlay on top of AVCaptureVideoPreviewLayer”

Social Share Toolbar

Swift: get values from Accelerometer and Gyroscope via CMMotionManager

To access data from Accelerometer and Gyroscope we are going to use CMMotionManager.
Here’s the class what wraps everything related to motion API (CMMotionManager).
And here’s the code:
Continue reading “Swift: get values from Accelerometer and Gyroscope via CMMotionManager”

Social Share Toolbar

swift: How to insert animated watermark into the video, or how to merge two videos

So I have a video from camera and array of images – result of screen capturing, basically the list of screenshots.
I want to merge them and add ability for User to save the resulted video on the phone. Let’s insert static image as a watermark as well as animated (video in video) watermark into the main video.

Continue reading “swift: How to insert animated watermark into the video, or how to merge two videos”

Social Share Toolbar

How to add pixel-perfect physics to an SKSpriteNode

To perfect the collision detection, basically to ignore transparency during the interest we can use SKPhysicsBody. The idea here is to have two images – one is what User sees, another is very simplified version for SpriteKit engine – simplified from color and geometry standpoint. The more complex “collision body” is the more resources it will spend during each frame to calculate collisions. Just for instance here’s the actual image and it’s “mask”

Now we are ready for some code
Continue reading “How to add pixel-perfect physics to an SKSpriteNode”

Social Share Toolbar

AWS DynamoDB query top 5 scores for country

Let’s take a look at how to query top 5 scores from AWS DynamoDB, let say we have some top scores table and want to get top 5 performers for the specific country. What we also need is not to waste the network resources, that’s why we will specify what exact fields we need to fetch from server – with help of projectionExpression.
Moreover let’s take a look at how to deal with the following error:
com.amazon.coral.validate#ValidationException”,”message”:”Invalid ProjectionExpression: Attribute name is a reserved keyword; reserved keyword: name”

it could be solved by adding reserved words to the expressionAttributeNames, e.g. instead of

queryExpression.projectionExpression = "userId,challenge,country,name,total"

where name and total are reserved words let’s do the following

queryExpression.expressionAttributeNames = [
            "#n": "name",
            "#t": "total",
queryExpression.projectionExpression = "userId,challenge,country,#n,#t"

That approach will eliminate the ValidationException – reserved keyword; reserved keyword error.

Here’s the whole method
Continue reading “AWS DynamoDB query top 5 scores for country”

Social Share Toolbar

Swift: How to create iOS Today extension and share data with containing app

Do you happen had a chance to see the 3d touch icon widget or iOS Today Extension, let’s take a look at how to create an Extension in swift, how to open containing app and how to share data with containing app.

To add Today Extension to the project click “file” -> “new” -> “target”: chose “Today Extension”.
Let’s go to “Capabilities” tab and enable “App Groups” for both AppWithExtension and Extension targets, for both app and extension you need to click “+” icon under the “App Groups” and add Groups to you app id, e.g. like here

Continue reading “Swift: How to create iOS Today extension and share data with containing app”

Social Share Toolbar

Swift: how to use 3D Touch – Introduction

3D Touch or force touch – is a new feature that have been introduced in the iPhone 6s. It’s interesting to know that you can get touch force, it is a number from 0 up to 6.6667, actually it a number from 0 up to maximumPossibleForce.

Let’s take a look at how to check if 3d touch is supported on current device:

func is3dTouchAvailable(traitCollection: UITraitCollection) -> Bool {
    return traitCollection.forceTouchCapability == UIForceTouchCapability.available
// Let's call it somewhere from the GameScene for instance
override func didMove(to view: SKView) {
    if(is3dTouchAvailable(traitCollection: self.view!.traitCollection)) {

Feel free to take a look at the example project – I’ll provide the link in the end of this post.
So this is a good practice to check whether 3d touch is supported or not.

Here’s an example of how to get the force of the 3d touch

func touchMoved(touch: UITouch, toPoint pos: CGPoint) {
    let location = touch.location(in: self)
    let node = self.atPoint(location)
    if is3dTouchEnabled {
        bubble.setPressure(pressurePercent: touch.force / touch.maximumPossibleForce)
    } else {
        // It is important to use touchMoved for iPhones w/o 3dTouch,
        // So we can "emulate" it.
        bubble.setPressure(pressurePercent: 1)

The current value of the Touch’s force is touch.force and the touch.maximumPossibleForce is a good one to convert force value to the “percentage”.
This code is from the small example of how to “emulate” the bubble reacting on the force of the touch.
To try this on your iPhone 6s/7 you can install this bubble wrap app real quick.

Here is my previous post with more details about Haptic feedback.

Social Share Toolbar