Giter Club home page Giter Club logo

mbjoystick's Introduction

MBJoyStick

Comstom UIView

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches) {
        let location = touch.location(in: self)
        if (ball.frame.contains(location)) {
            stickActive = true
        }
        else {
            stickActive = false
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches) {
        var location = touch.location(in: self)
        let selfCenter = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
        let selfRadius = self.frame.size.width/2
        let distance = distanceBetweenTwoPoints(point1: location, point2: selfCenter)
        if (distance > selfRadius) {
            let vX = location.x - selfCenter.x
            let vY = location.y - selfCenter.y
            location.x = selfCenter.x + ((vX / distance) * selfRadius)
            location.y = selfCenter.y + ((vY / distance) * selfRadius)
        }
        ball.center = location
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if (stickActive==true) {
        UIView.animate(withDuration: 0.3, animations: {
            self.ball.center = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
        })
    }
}

func distanceBetweenTwoPoints(point1:CGPoint,point2:CGPoint)-> CGFloat {
    let dx = point2.x - point1.x
    let dy = point2.y - point1.y
    return sqrt(dx * dx + dy * dy)
}

mbjoystick's People

Contributors

icoder86 avatar mehul86 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.