Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // GameScreen.swift
- // LineRider
- //
- // Created by Andrew Solesa on 2017-04-14.
- // Copyright © 2017 KSG. All rights reserved.
- //
- import UIKit
- class GameScreen: UIScrollView
- {
- // LINE VARIABLES
- var lastPoint = CGPoint.zero
- var red: CGFloat = 0.0
- var green: CGFloat = 0.0
- var blue: CGFloat = 0.0
- var brushWidth: CGFloat = 10.0
- var opacity: CGFloat = 1.0
- var swiped = false
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
- {
- print("TOUCHED")
- swiped = false
- if let touch = touches.first
- {
- lastPoint = touch.location(in: self)
- }
- }
- func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint)
- {
- UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0)
- let context = UIGraphicsGetCurrentContext()
- context?.move(to: fromPoint)
- context?.addLine(to: toPoint)
- context?.setLineCap(CGLineCap.round)
- context?.setLineWidth(brushWidth)
- context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
- context?.setBlendMode(CGBlendMode.normal)
- context?.strokePath()
- UIGraphicsEndImageContext()
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
- {
- print("MOVED")
- swiped = true
- if let touch = touches.first
- {
- print("CREATING POINT")
- let currentPoint = touch.location(in: self)
- drawLine(from: lastPoint, to: currentPoint)
- lastPoint = currentPoint
- }
- }
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
- {
- if !swiped
- {
- self.drawLine(from: lastPoint, to: lastPoint)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment