Notice
Recent Posts
Recent Comments
Link
관리 메뉴

김종권의 iOS 앱 개발 알아가기

[iOS - swift] CGRect의 divided(atDistance:from:) 메소드 (CGRect slice 방법) 본문

iOS 기본 (swift)

[iOS - swift] CGRect의 divided(atDistance:from:) 메소드 (CGRect slice 방법)

jake-kim 2021. 8. 30. 23:41

사각형을 자르고 싶은 경우, divided(atDistance:from:) 사용

  • from의 좌표로 부터 adDistance 떨어진 것으로 사각형 slice
  • 하나의 사각형(CGRect)을 두 개의 사각형(CGRect)로 slice하는 코드

  • Rect객체에서 divided(atDistance:from:)을 사용
// .minXEdge 의미: x좌표의 시작점을 기준으로 30의 길이로 slice
let slices = originRect.divided(atDistance: 30.0, from: .minXEdge)
let slicedRect = slices.slice
let remainderRect = slices.remainder

sliceView.frame = slices.slice
remainderView.frame = slices.remainder
원본 사각형 x좌표 마지막으로부터 30떨어진 사각형 2개로 slice x좌표 처음으로부터 30 떨어진 사각형 2개로 slice 
  .divided(atDistance: 30.0,
from: .maxXEdge)
.divided(atDistance: 30.0,
from: .minXEdge)
  • y좌표 기준 slice
y좌표 마지막으로부터 30떨어진 사각형 2개로 slice y좌표 처음으로부터 30 떨어진 사각형 2개로 slice 
.divided(atDistance: 30.0, from: .maxYEdge) .divided(atDistance: 30.0, from: .minYEdge)

 
 

* 참고

- https://developer.apple.com/documentation/coregraphics/cgrect/2299988-divided

Comments