Last active
August 19, 2019 11:45
-
-
Save vikaskore/bfb517282df666b598d07ca12c68dd62 to your computer and use it in GitHub Desktop.
Touches Moved and Touches Ended functions to animation user selections according to their touch location in swift iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GesturesViewController.swift | |
// Gestures Demo | |
// | |
// Created by VikasK on 18/08/19. | |
// Copyright © 2019 VikasKore Software. All rights reserved. | |
// | |
import UIKit | |
class GesturesViewController: UIViewController { | |
//Label to display selected value | |
@IBOutlet var lblScore: UILabel! | |
/* | |
UIView where in we place collection of button, | |
also helps to identify touch location contain within. | |
helpful to minimise constraint to set or to border to look better and show different | |
*/ | |
@IBOutlet var scoreView: UIView! | |
//Array of buttons | |
@IBOutlet var scoreOptions: [UIButton]! | |
//To hide status bar(optional) | |
override var prefersStatusBarHidden: Bool { | |
return true | |
} | |
/* | |
Animate user selection while moving finger on buttons | |
*/ | |
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let touch = touches.first { | |
let touchpoint:CGPoint = touch.location(in: self.view) | |
if scoreView.frame.contains(touchpoint) { | |
//returns touch location respective to score view | |
let position = touch.location(in: scoreView) | |
print(position) | |
//For loop to find out which button is at touch location | |
for button in scoreOptions { | |
if button.frame.contains(position) { | |
//Clear all selections | |
_ = scoreOptions.map {$0.setBorder(withColor: .clear, borderWidth: 1.0, cornerRadius: button.frame.height/2) } | |
//set border to selected button | |
button.setBorder(withColor: .green, borderWidth: 1.0, cornerRadius: button.frame.height/2) | |
//Pass button tag to function | |
scoreSelected(button.tag) | |
} | |
} | |
} | |
} | |
} | |
/* | |
Animate user selection while tapping on a buttons | |
*/ | |
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let touch = touches.first { | |
let touchpoint:CGPoint = touch.location(in: self.view) | |
if scoreView.frame.contains(touchpoint) { | |
let position = touch.location(in: scoreView) | |
print(position) | |
for button in scoreOptions { | |
if button.frame.contains(position) { | |
_ = scoreOptions.map {$0.setBorder(withColor: .clear, borderWidth: 1.0, cornerRadius: button.frame.height/2) } | |
button.setBorder(withColor: .green, borderWidth: 1.0, cornerRadius: button.frame.height/2) | |
scoreSelected(button.tag) | |
} | |
} | |
} | |
} | |
} | |
//Update selected value on label | |
func scoreSelected(_ id : Int) { | |
lblScore.text = String(id) // "\(id)" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment