나는 내 자신의 연습을 위해 간단한 단일보기 응용 프로그램을 만들려고 노력하고 있지만 문제가 발생하여 지난 며칠 동안이 문제를 해결하지 못했습니다. 나는이 스타일의 버튼 중 하나를 누르거나 슬라이더를 조정하면 Swift에서 이미 그려진 CGContext를 다시 그립니다.
Screenshot of the current program
기본적으로, 나는 그것이 화면 (빨간 점선)의 하단에 년대 CGContext를 다시 그려야합니다. 나는 STYLE 1 버튼을 누르면 예를 들어,이 drawStyleOne (호출 한 함수)를 호출하고이 두 개의 서로 다른 변경 적용해야합니다 :context?.setLineDash(phase: 0, lengths: [2,3])
context?.setStrokeColor(UIColor.green.cgColor)
내 목표는 다시 그리는 점선 (작은 점)에 있고 변화를 컨텍스트의 색은 녹색 (컨텍스트는 CGContext의 인스턴스입니다.)
모든 것은 override func draw (_ rect : CGRect)에 그려져 있습니다. 그러나 원하는대로 CGContext를 다시 그릴 수는 없습니다.
다음은이 상황을 좀더 자세히 설명하는 데 도움이되는 내용입니다.
import UIKit
class VectorView: UIView {
private var context: CGContext? = nil
override init(frame: CGRect) {
super.init(frame: frame)
//contentMode = .redraw //tried this, didn't work.
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
// lets us make calls
context = UIGraphicsGetCurrentContext()!
context?.move(to: CGPoint(x: 0.0, y: 10.0))
context?.setLineDash(phase: 0, lengths: [2,3])
context?.setLineJoin(CGLineJoin.bevel)
context?.setLineCap(CGLineCap.square)
context?.addLine(to: CGPoint(x: 50.0, y: 70.0))
context?.addLine(to: CGPoint(x: 100.0, y: 20.0))
context?.setLineWidth(1.0)
context?.setStrokeColor(UIColor.red.cgColor)
context?.drawPath(using: CGPathDrawingMode.stroke)
}
func drawStyleOne() {
context = UIGraphicsGetCurrentContext()
context?.move(to: CGPoint(x: 0.0, y: 10.0))
context?.setLineDash(phase: 0, lengths: [2,3])
context?.setLineJoin(CGLineJoin.bevel)
context?.setLineCap(CGLineCap.square)
context?.addLine(to: CGPoint(x: 50.0, y: 70.0))
context?.addLine(to: CGPoint(x: 100.0, y: 20.0))
context?.setStrokeColor(UIColor.green.cgColor)
context?.drawPath(using: CGPathDrawingMode.stroke)
self.setNeedsDisplay()
//contentMode = .redraw //Tried this and didn't work either..
NSLog("drawStyleOne got called")
}
위의 클래스에서 drawStyleOne 함수가 AppDelegate 내부에서 호출됩니다.
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let screenSize = UIScreen.main.bounds
private var _vectorView: VectorView? = nil
private var _buttonStylesView: ButtonStyleView? = nil
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.rootViewController = ViewController()
window?.rootViewController?.view.backgroundColor = UIColor.white
window?.makeKeyAndVisible()
_vectorView = VectorView()
_vectorView?.frame = CGRect(x: 0.0, y: 650, width: 414, height: 70)
_vectorView?.backgroundColor = UIColor.lightGray
window?.rootViewController?.view.addSubview(_vectorView!)
_buttonStylesView = ButtonStyleView()
_buttonStylesView?.frame = CGRect (x: screenSize.width/10, y: screenSize.height * 6/10, width: 414, height: 100)
_buttonStylesView?.backgroundColor = UIColor.clear
window?.rootViewController?.view.addSubview(_buttonStylesView!)
_buttonStylesView?.styleOne?.addTarget(self, action: #selector(styleButtonPressed), for: UIControlEvents.touchDown)
return true
}
func styleButtonPressed() {
NSLog("STYLE BUTTON PRESSED")
_vectorView?.drawStyleOne()
}
}
, 그러나 그것은 아무리 내가 무엇을하려고 줄을 다시 그리기하지 않습니다 난 그냥 함수가 호출지고 있는지 확인하기 위해 마지막에 ("drawStyleOne 호출있어") NSLog를 넣어 (그리고는 않습니다).
도움/조언을 많이 주시면 감사하겠습니다.
답변 해 주셔서 감사합니다. 나는 이것을 투표하는 것을 너무 좋아하지만, 지금 당장에는 충분한 평판 포인트가 없다. ... ( – whgnsdlr7
@ whgnsdlr7 - 정답으로 받아 들일 수 있어야한다. 투표를하기 위해 담당자에게!) :) – Grimxn