我经常需要显示一个 uiscrollview,其中包含可以缩放的图像。这是我在任何特定应用程序中使用的许多其他 ScrollView 之上的。
我想将其分解为自己的类,我可以实例化它,例如:
CustomScrollView *scr = [CustomScrollView alloc] init];
scr.image = [UIImage imageNamed:@"myImage.png"];
scr.doesPinchZoom = YES;
CustomScrollView 应创建一个 uiscrollview,其中包含允许捏合和缩放的图像。
这也有自己的关闭按钮来删除所述 ScrollView 。 我的代码现在甚至无法创建 ScrollView 。
@interface CustomScrollView () <UIScrollViewDelegate>
@property (nonatomic, strong, readonly) UIScrollView *scrollView;
@end
@implementation CustomScrollView
@synthesize scrollView = _scrollView;
- (UIScrollView *)scrollView {
if (nil == _scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.delegate = self;
[_scrollView setBackgroundColor:[UIColor redColor]];
[self addSubview:_scrollView];
NSLog(@"scrollview");
}
return _scrollView;
}
沿着这条路走有什么方向吗?或者甚至只是让 ScrollView 显示出来...... 当我使用上面的分配实例化 ScrollView 时, ScrollView 甚至没有显示在我的 View Controller 中。
最佳答案
我不知道您发布的代码是否应该是 .h/.m 文件的组合,但无论如何我相信,除非我误解了您,否则您正在尝试创建 的子类一个 UIScrollView 对象。您绝对可以这样做来自定义 UIScrollView 并使其在许多情况下可重用。
如果将子类命名为 CustomScrollView,示例头文件将如下所示:
//
// CustomScrollView.h
//
#import <UIKit/UIKit.h>
@interface CustomScrollView : UIScrollView
@property (strong, nonatomic) UIImageView *theImage;
@end
以及您的实现文件:
//
// CustomScrollView.m
#import "CustomScrollView.h"
@implementation CustomScrollView
@synthesize theImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
theImage = nil;
}
return self;
}
-(void)setTheImage:(UIImageView*)image{
theImage = image;
[self addSubview:theImage];
}
@end
然后,无论您想在哪里使用新的自定义 ScrollView,您都可以这样做:
CustomScrollView *cSV = [[CustomScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
cSV.delegate = self;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.image = [UIImage imageNamed:@"yourPic.png"];
[cSV setTheImage:imageView];
[self.view addSubview:cSV];
(我做了随机帧,你可以将它们设置为你想要的)
从那里你可以创建类方法来完成你想做的其他事情
希望这在某种程度上有所帮助
关于ios - 可重用的 uiscrollview 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17349375/