Touch and Hold Swift

written in ios, swift, technical

Recently, at VID, I created a functional iPad application for a client’s property platform. One function was panning a 360 image on a Windows application.

Prior to working on these Swift methods our team created a Windows launcher and API to connect with and show these images on the screen. One of the images that needed panoramic capability were the floor views. In the Main.storyboard file I created one View Controller for these API calls. As you can see each view has a left and right arrow which will repeat call the API to move left or right.

To be brief, here is one variable of the first right button on the Main.storyboard as well as the leftTimer and rightTimer variables that will be used in the future “touch and hold” methods.

[game.rb]
1
2
3
4
  @IBOutlet weak var right1Button: UIButton!

  var leftTimer : NSTimer? = nil
  var rightTimer : NSTimer? = nil

The leftButtonDown and rightButtonDown methods repeatedly call the moveLeft or moveRight methods every 0.1 second while the user continues to press the view button. Once the user lifts their finger up which is considered a Touch Up type of Swift method the timer becomes invalid and panoramic capability ceases.

[game.rb]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//  PANORAMIC CAPABILITY
    @IBAction func leftButtonDown(sender: UIButton) {
        leftTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "moveLeft", userInfo: nil, repeats: true)
    }

    @IBAction func leftButtonUp(sender: UIButton) {
        leftTimer!.invalidate()
    }

    @IBAction func rightButtonDown(sender: UIButton){
        rightTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "moveRight", userInfo: nil, repeats: true)
    }

    @IBAction func rightButtonUp(sender: UIButton){
        rightTimer!.invalidate()
    }

The moveLeft and moveRight methods are simple HTTP GET requests using a Swift NSMutableURLRequest(). In the Windows API this URL call will move the image a small percentage to the left or right, together with the panoramic capability methods this ensures a panoramic view without breaks.

[game.rb]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//    VIEW URL CALL
    func moveLeft() {
        let request = NSMutableURLRequest(URL: NSURL(string: "http://\(url!)/BuildingViewer/rest/MoveLeft")!)
        request.HTTPMethod = "GET"
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in

            if error != nil {
                print("error=\(error)")
                return
            }

            print("response = \(response)")
            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()
    }

    func moveRight() {
        let request = NSMutableURLRequest(URL: NSURL(string: "http://\(url!)/BuildingViewer/rest/MoveRight")!)
        request.HTTPMethod = "GET"
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in
            if error != nil {
                print("error=\(error)")
                return
            }
            print("response = \(response)")

            let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("responseString = \(responseString)")
        }
        task.resume()
    }

All that’s left is to connect the buttons with the methods we just created. Again, to be brief I only am showing the first right button but were we to look at all the buttons, the right buttons would look exactly like this while the left buttons would connect to the leftButtonUp and leftButtonDown methods.

Keep on being badass programmers!


Comments