Software Development

Example of using extensions in Swift

Recently I needed a image in my iOS app that was oscillating. I ran into an example of Swift code that showed how to do it and it was actually quite easy to do (for the theory behind the formula see this). The code makes use of the ‘extension’ feature in Swift. This is a really cool feature in Swift. The feature allows you to extend the functionality of a class without having to extend the class (like you would do in Java for instance). For a basic example of the feature see this example. In my example the feature is used to extend the functionality of the SKAction class, another cool thing in the iOS SpriteKit library.

The extension looks like this:

01
02
03
04
05
06
07
08
09
10
11
let π = CGFloat(M_PI)
 
extension SKAction {
    static func oscillate(amplitude a: CGFloat, timePeriod t: CGFloat, midPoint: CGPoint) -> SKAction {
        let action = SKAction.customActionWithDuration(Double(t)) { node, currentTime in
            let displacement = a * sin(2 * π * currentTime / t)
            node.position.y = midPoint.y - displacement
        }
        return action
    }
}

You can use the extension and apply it to a SpriteNode in your GameScene.swift for example like this:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import SpriteKit
 
class GameScene: SKScene {
     
    let ball = SKSpriteNode(imageNamed: "ball")
     
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        backgroundColor = SKColor.clearColor()
         
        ball.position = CGPoint(x: size.width * 0.5, y: size.height * 0.5)
         
        addChild(ball)
         
        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width)
        ball.physicsBody?.restitution = 0.0
        ball.physicsBody?.linearDamping = 0.0
        ball.size = CGSize(width: 100, height: 100)
        ball.physicsBody?.affectedByGravity = false
         
        let oscillate = SKAction.oscillation(amplitude: size.height/4, timePeriod: 8, midPoint: ball.position)
        ball.runAction(SKAction.repeatActionForever(oscillate))
    }
}

As you can see I ignored the gravity and other ‘forces’ on the object. This results in the following behaviour:bouncing-ball1

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Pascal Alma

Pascal is a senior JEE Developer and Architect at 4Synergy in The Netherlands. Pascal has been designing and building J2EE applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Specialties: JEE, SOA, Mule ESB, Maven, Cloud Technology, Amazon AWS.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button