SwiftUI: Difference between revisions
Jump to navigation
Jump to search
Line 203: | Line 203: | ||
* [https://www.hackingwithswift.com/quick-start/swiftui/whats-the-difference-between-observedobject-state-and-environmentobject <code>@ObservedObject</code>, <code>@State</code> vs. <code>@EnvironmentObject</code>] | * [https://www.hackingwithswift.com/quick-start/swiftui/whats-the-difference-between-observedobject-state-and-environmentobject <code>@ObservedObject</code>, <code>@State</code> vs. <code>@EnvironmentObject</code>] | ||
* [https://stackoverflow.com/questions/54971163 Set environment variable <code>CG_PDF_VERBOSE</code>] | * [https://stackoverflow.com/questions/54971163 Set environment variable <code>CG_PDF_VERBOSE</code>] | ||
* [https://www.hackingwithswift.com/books/ios-swiftui/wrapping-a-uiviewcontroller-in-a-swiftui-view Wrapping a UIViewController in a SwiftUI] | |||
* [https://medium.com/codespace69/xcode-10-xcode-11-2-x-error-multiple-commands-produce-4e5ab75558f2 Error: Multiple commands produce] | * [https://medium.com/codespace69/xcode-10-xcode-11-2-x-error-multiple-commands-produce-4e5ab75558f2 Error: Multiple commands produce] | ||
* [https://stackoverflow.com/questions/748175 Asynchronous vs Synchronous] | * [https://stackoverflow.com/questions/748175 Asynchronous vs Synchronous] |
Revision as of 01:35, 13 May 2020
ContentView
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
struct ContentView_Preview: PreviewProvider {
static var previews: some View {
ContentView()
}
}
MyPDFView
// MARK: - MyPDFView
fileprivate struct MyPDFView: UIViewRepresentable {
@Binding var data: Data
func makeUIView(context: UIViewRepresentableContext<MyPDFView>) -> UIView {
let empty = UIView()
//guard let provider = CGDataProvider(data: self.data as CFData) else {return empty}
//guard let document = CGPDFDocument(provider) else {return empty}
guard let url = URL(string: "https://api.chorke.org/academia/api/v1.0/health/card/01890600.pdf") else {return empty}
guard let document = CGPDFDocument(url as CFURL) else {return empty}
guard let page = document.page(at: 1) else {return empty}
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let uiImage = renderer.image { ctx in
UIColor.white.set()
ctx.fill(pageRect)
ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
ctx.cgContext.drawPDFPage(page)
}
let view = UIImageView(image: uiImage)
view.contentMode = .scaleAspectFill
return view
}
func updateUIView(_ view: UIView, context: UIViewRepresentableContext<MyPDFView>) {
// MARK: - TODO
}
}
MyKDCalendar
import SwiftUI
import KDCalendar
public struct MyKDCalendar: UIViewRepresentable {
var date: Date = .init()
public func makeUIView(context: Context) -> CalendarView {
let calendar: CalendarView = .init()
calendar.setDisplayDate(self.date, animated: false)
calendar.selectDate(self.date)
calendar.dataSource = self
calendar.delegate = self
return calendar;
}
public func updateUIView(_ calendar: CalendarView, context: Context) {
calendar.selectDate(self.date)
}
}
extension MyKDCalendar: CalendarViewDataSource {
public func startDate() -> Date {
return Date.init()
}
public func endDate() -> Date {
var month:DateComponents = .init()
month.month = 2
return Calendar.current.date(byAdding: month, to: Date.init())!
}
public func headerString(_ date: Date) -> String? {
return nil
}
}
extension MyKDCalendar: CalendarViewDelegate {
public func calendar(_ calendar: CalendarView, didDeselectDate date: Date) {}
public func calendar(_ calendar: CalendarView, didScrollToMonth date: Date) {}
public func calendar(_ calendar: CalendarView, didSelectDate date: Date, withEvents events: [CalendarEvent]) {}
public func calendar(_ calendar: CalendarView, didLongPressDate date: Date, withEvents events: [CalendarEvent]?) {}
public func calendar(_ calendar: CalendarView, canSelectDate date: Date) -> Bool {
return true
}
}