在 Swift 中使用 SPUserResizableView

Using SPUserResizableView in Swift

我正在尝试使用 Spoletto 在 github (https://github.com/spoletto/SPUserResizableView) 中创建的 SPUserResizableView。

我导入了 .h 和 .m 文件,创建了一个桥并将委托添加到我的控制器。

我认为问题是这个库太旧了,这就是为什么我在 .h 和 .m 文件中遇到很多错误,所以我不能使用这个库。

错误 - http://postimg.org/gallery/3bq2ldo0m/

你能帮我设置 swift 中的库吗?

您需要设置一个 Objective-C 桥接头,然后将其导入其中。为此:

  1. 将 .h 和 .m SPUserResizableView 文件拖放到您的项目中。
  2. 当Xcode询问您是否要创建桥接头时,select 'Yes'。
  3. 在名为 'YOUR-PROJECT-NAME-bridging-header.h' 的新文件中键入此代码: <code>#import SPUserResizableView.h

  4. 您现在应该可以在 Swift 中使用它了!

我也遇到同样的错误,把这两个加在SPUserResizableView.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

您的问题将得到解决。

我使用 Swift 4 通过将变量声明从接口移动到实现,并删除了释放和释放代码。

SPUserResizableView.h改为

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

typedef struct SPUserResizableViewAnchorPoint {
    CGFloat adjustsX;
    CGFloat adjustsY;
    CGFloat adjustsH;
    CGFloat adjustsW;
} SPUserResizableViewAnchorPoint;

@protocol SPUserResizableViewDelegate;
@class SPGripViewBorderView;

@interface SPUserResizableView : UIView {
}

//...
//(identical from this point on)

SPUserResizableView.m改为

    //...
    //(identical up to)

            //CGColorSpaceRelease(baseSpace), baseSpace = NULL;
            CGColorSpaceRelease(baseSpace);
        baseSpace = NULL;


        @implementation SPUserResizableView

    SPGripViewBorderView *borderView;
    UIView *contentView;
    CGPoint touchStart;
    CGFloat minWidth;
    CGFloat minHeight;

    // Used to determine which components of the bounds we'll be modifying, based upon where the user's touch started.
    SPUserResizableViewAnchorPoint anchorPoint;

    id <SPUserResizableViewDelegate> delegate;


    //@synthesize contentView, minWidth, minHeight, preventsPositionOutsideSuperview, delegate;


    //...
    //(this part identical)



    - (void)dealloc {
        [contentView removeFromSuperview];
    }

    @end