introduce

A toy that uses Objective-C to implement a Swiftui-like interface development framework.

Directions for use

The syntax and naming basically follow SwiftUI, and you can create simple applications using the interface of the framework.

1. Create an application entry point

Remove the main function in the original OC project, and implement the OCAppProtocol protocol in your App proxy object (there is only one). Such as:

@interface DemoApp : NSObject <OCAppProtocol> @end @implementation DemoApp - (OCScene *)body { return OCWindowGroup(^{ DemoView(); }).onChange(^(OCScenePhase scenePhase, OCScenePhase newScenePhase) { switch(newScenePhase) { case OCScenePhaseBackground: break; case OCScenePhaseActive: break; case OCScenePhaseInactive: break; }}); } @endCopy the code

It is also possible to create a separate interface that Bridges with the original OC project via the OCHostingController method:

OCHostingController(view, ^(UIViewController *vc) {
    // ...
});
Copy the code

Use of view elements

Basically consistent with SwiftUI. Inherit the OCView class and implement the body method, for example:

- (OCView *)body { return TabView(^{ NavigationView(^{ ScrollView(^{ Image(@"logo") .resizable(UIEdgeInsetsZero, UIImageResizingModeTile) .aspectRatio(UIViewContentModeScaleAspectFit) .frame(200, 50, OCAlignmentCenter); HStack(^{Text(@" Text "); Button(@" Button ", nil); TextField(@" input box ", $(self->_text), nil, nil); TextEditor($(self->_text)); }); WebView([NSURL URLWithString:@"https://www.baidu.com/"]) .frame(300, 210, OCAlignmentCenter); for (int i = 0; i < 50; i++) { HStack(^{ Text(@(i).stringValue); Spacer(); }); } }).tabItem(^{ Image(@"circle.fill"); Text(@" layout demo "); }). NavigationBarTitle1 (@" layout demo "); }); }); }Copy the code

3. Data binding

Only State and Binding are currently implemented. Slightly different from SwiftUI, the State modifier is implemented through the State() method when class members are initialized, and variables are updated through the StateUpdate() method. The Binding modifier uses $(), for example:

- (OCView *)body {return TabView($(_selection), ^{VStack(^{TextField(@" Enter text here ", $(self->_text), ^{NavigationView(^{VStack(^{TextField(@" enter text here ", $(self->_text), nil, ^{ [UIApplication.sharedApplication.keyWindow endEditing:YES]; }).background(OCColor.lightGrayColor); HStack(^{Text([NSString stringWithFormat:@" %ld", self->_text.length]); Button(@" clear ", ^{StateUpdate(self->_text, nil); }); }); NavigationLink(SubViewMake($(self->_isActive2)), $(self->_isActive2), ^{Text(@" 查 自 页 "); }); }).tabItem(^{ Image(@"pentagon.fill"); Text(@" data binding "); }); }). NavigationBarTitle1 (@" data binding "); }); }Copy the code

instructions

This frame is just a toy and certainly not as powerful as SwiftUI and is not recommended for use in real production environments. In addition, open source projects will inevitably be exploited and commercialized, so open source is not considered for the time being.