iOS: Apple Pencil 터치
                                            
                                                
                                                
                                                
                                                
                                                
                                                 10435 단어  SwiftiPadProApplePencil
                    
사용하기에 매우 편리하다.
Apple Pencil 애플리케이션을 어떻게 만드는지 생각 중이에요.
나는 어떤 강좌 같은 것이 있는지 찾다가 다음과 같은 사이트를 발견했다.
⬇︎
https://www.raywenderlich.com/121834/apple-pencil-tutorial
※ 영어가 안 되면 틀릴 수 있습니다.
Download and explore Scribble."###"을 클릭하면 자습서에 사용되는 항목이 줄어듭니다.
이 프로젝트를 실행하면 밋밋한 선을 그릴 수 있을 것 같다.
 
 1항: 펜의 압력에 따라 선의 굵기를 바꾼다
private func lineWidthForDrawing(context: CGContext?, touch: UITouch) -> CGFloat {
    var lineWidth = defaultLineWidth
    if touch.force > 0 {
        lineWidth = touch.force * forceSensitivity
    }
    return lineWidth
  }
if touch.force > 0 {
  lineWidth = touch.force * Forcefulness
}
 
 다음: 더 높은 수준에서 그리기
아이패드 프로가 터치 이벤트를 스캔하는 시간은 120초라고 한다.
하지만 아이패드 프로의 주사율이 60이기 때문에 다 주울 수는 없어요.
원 같은 글자를 그리면 각질이 있는 원이 된다.
그곳에서 PG 측에서 보내온 신호를 정리해 원활하게 쓰도록 했다.(번역에 자신이 없음)
drawStroke(context, touch: touch)
var touches = [UITouch]()
    // 2
    if let coalescedTouches = event?.coalescedTouchesForTouch(touch) {
        touches = coalescedTouches
    } else {
        touches.append(touch)
    }
    // 3
    print(touches.count)
    // 4
    for touch in touches {
        drawStroke(context, touch: touch)
    }
수정할 전면 원
 
 수정된 원
 
 멀리서 보면 이해하기 어렵지만 눈의 착각이 아니라면 큰 변화가 있을 것이다.(그림만 잘 그리지 못하는...)
머리에 열이 좀 났어요. 다음은 마지막이에요.
마지막으로 ApplePencil의 기울기를 체크하고 각도에 따라 그리기 방법을 변경합니다.
중도이런 견해가 있다.
4
pencilTexture.setStroke() 이와 같이 변경자산으로 등록된 이미지로 선을 그을 수 있다.
private var pencilTexture = UIColor(patternImage: UIImage(named: "PencilTexture")!)
그리고 본론.
drawStroke(){
var lineWidth:CGFloat
    if touch.altitudeAngle < tiltThreshold {
        lineWidth = lineWidthForShading(context, touch: touch)
    } else {
        lineWidth = lineWidthForDrawing(context, touch: touch)
    }
 private func lineWidthForShading(context: CGContext?, touch: UITouch) -> CGFloat {
        // 1
        let previousLocation = touch.previousLocationInView(self)
        let location = touch.locationInView(self)
        // 2 - vector1 is the pencil direction
        let vector1 = touch.azimuthUnitVectorInView(self)
        // 3 - vector2 is the stroke direction
        let vector2 = CGPoint(x: location.x - previousLocation.x, y: location.y - previousLocation.y)
        // 4 - Angle difference between the two vectors
        var angle = abs(atan2(vector2.y, vector2.x) - atan2(vector1.dy, vector1.dx))
        // 5
        if angle > π {
            angle = 2 * π - angle
        }
        if angle > π / 2 {
            angle = π - angle
        }
        // 6
        let minAngle: CGFloat = 0
        let maxAngle = π / 2
        let normalizedAngle = (angle - minAngle) / (maxAngle - minAngle)
        // 7
        let maxLineWidth: CGFloat = 60
        var lineWidth = maxLineWidth * normalizedAngle
        return lineWidth
    }
 
 확실히 기울어진 부분에 변화가 있어요!
펜을 기울여 쓰는 게임을 할 수 있다면 재미있을 것이다.
머리가 극한이니까 만지는 건 여기서 끝이야.
Reference
이 문제에 관하여(iOS: Apple Pencil 터치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/steelhearts/items/138af07071af90bc3c38텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)