【发布时间】:2019-12-20 18:23:43
【问题描述】:
我必须在 Objective-C 项目中实现骨架效果。我在 Objc 上找到了很多库,但它们没有执行我想做的事情。我找到了这个 Swift 库——https://github.com/Juanpe/SkeletonView。 我使用带有此扩展的桥接头使其与任何 UIView 一起使用。
快速文件
extension UIView {
@objc public func showWaitingLoader() {
let gradient = SkeletonGradient(baseColor: UIColor(red:0.9, green:0.9, blue:0.9, alpha:1))
let animation = SkeletonAnimationBuilder().makeSlidingAnimation(withDirection: GradientDirection.leftRight)
self.showAnimatedGradientSkeleton(usingGradient: gradient, animation: animation)
}
@objc public func hideWaitingLoader(){
self.hideSkeleton(reloadDataAfter: true)
}
}
Objective-C 文件
#import "ViewController.h"
#import "SkeletonView-Swift.h"
#import "Skeleton-Swift.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;
@property (weak, nonatomic) IBOutlet UIView *container;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
- (void)viewDidLoad {
[super viewDidLoad];
_firstLabel.isSkeletonable = YES;
[_container showWaitingLoader];
}
效果非常好。但是现在我需要一个集合(TableView)中的这个效果。该库说要使视图控制器符合下一个协议,即下一个。
public protocol SkeletonTableViewDataSource: UITableViewDataSource {
func numSections(in collectionSkeletonView: UITableView) -> Int
func collectionSkeletonView(_ skeletonView: UITableView, numberOfRowsInSection section: Int) -> Int
func collectionSkeletonView(_ skeletonView: UITableView, cellIdentifierForRowAt indexPath: IndexPath) -> ReusableCellIdentifier
}
但我不知道如何在我的头文件(Objective-C 项目)上使用此协议。像这样
#import <UIKit/UIKit.h>
#import "SkeletonView-Swift.h"
#import "Skeleton-Swift.h"
@interface ViewController : UIViewController <SkeletonTableViewDataSource>
@end
根据上一个问题,显然我不能在我的头文件中使用这个协议。
Can an objective-c class conform to a swift protocol?
所以我想知道如何使用这个协议,以便在我的 ViewController.m 中符合它并在我的 tableview 中具有骨架效果。
【问题讨论】:
-
您必须使用
@protocol声明。 -
你介意提供一个例子吗:D?
标签: ios objective-c swift