在swift中实现Objective-C Framework(TOCropViewController)方法

时间:2017-02-02 13:57:34

标签: ios objective-c swift bridging-header

我正在尝试实现一个用CocoaPods(TOCropViewController)编写的框架,并将其导入bridging-header中,如下所示:

桥接标题

#import <TOCropViewController/TOCropViewController.h>
#import <TOCropViewController/TOCropView.h>
#import <TOCropViewController/TOCropToolbar.h>
#import <TOCropViewController/TOCropOverlayView.h>

我面临的问题是我不了解Objective-C,因此我不知道如何在Swift中实现这个框架。这是在其页面上记录的基本实现:

- (void)presentCropViewController
{
UIImage *image = ...; //Load an image

TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
cropViewController.delegate = self;
[self presentViewController:cropViewController animated:YES completion:nil];
}

- (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle
{
// 'image' is the newly cropped version of the original image
} 

我尝试在Swift中这样做:

 self.presentCropViewController{
    }

但正如预期的那样,它给我一个错误,指出ViewController的值没有成员presentCropViewController

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

以下代码将有助于翻译,尝试理解Swift和Objective-C之间的关系,以便将来可以这样做。

func presentCropViewController() {
    let image = ...

    let cropViewController = TOCropViewController(image: image)
    cropViewController.delegate = self
    present(viewController: cropViewController, animated: true, completion: nil)
}

func cropViewController(_ cropViewController: TOCropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle angle: NSInteger) {

}

更多信息,请访问https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html