0

这个问题可能看起来很简单,但请听我说完。我犯了一个巨大的错误,我的项目在一个故事板中包含 300 多个活动,而我最近才发现 Xcode 在处理它方面很糟糕。所以我想做的是让我现在在单独的活动中显示的不同内容写入代码,这样我的情节提要中只需要最少数量的活动。这可能很难理解,我很抱歉。

在我看来,这是它应该如何工作的。我举个例子。

在我的应用程序中,我有一个名为 MAPS 的部分,在主页上单击地图按钮,它会将您带到一个菜单,其中包含带有地图名称的按钮。正如您所期望的那样,按钮上的地图名称会为您带来带有所述地图的活动。问题是每张地图都在一个单独的活动中,该活动也在情节提要内链接。

我想做的是以编程方式从我的故事板中的一个活动中调用每个单独的活动,有效地将 20 多个活动替换为 1 个。

这是我的地图视图控制器的样子(它们实际上是带有滚动视图的 .png 图像)

MapRoute1.h

#import "ViewController.h"

@interface MapRoute1 : ViewController <UIScrollViewDelegate>

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) UIImageView *imageView;

- (void)centerScrollViewContents;
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer;
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer;

@end

MapRoute1.m

    #import "MapRoute1.h"

@interface MapRoute1 ()

@end

@implementation MapRoute1

@synthesize imageView = _imageView;
@synthesize scrollView = _scrollView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)centerScrollViewContents {
    CGSize boundsSize = self.scrollView.bounds.size;
    CGRect contentsFrame = self.imageView.frame;

    if (contentsFrame.size.width < boundsSize.width) {
        contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
    } else {
        contentsFrame.origin.x = 0.0f;
    }

    if (contentsFrame.size.height < boundsSize.height) {
        contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
    } else {
        contentsFrame.origin.y = 0.0f;
    }

    self.imageView.frame = contentsFrame;
}

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer {
    // 1
    CGPoint pointInView = [recognizer locationInView:self.imageView];

    // 2
    CGFloat newZoomScale = self.scrollView.zoomScale * 1.5f;
    newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);

    // 3
    CGSize scrollViewSize = self.scrollView.bounds.size;

    CGFloat w = scrollViewSize.width / newZoomScale;
    CGFloat h = scrollViewSize.height / newZoomScale;
    CGFloat x = pointInView.x - (w / 2.0f);
    CGFloat y = pointInView.y - (h / 2.0f);

    CGRect rectToZoomTo = CGRectMake(x, y, w, h);

    // 4
    [self.scrollView zoomToRect:rectToZoomTo animated:YES];
}

- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer*)recognizer {
    // Zoom out slightly, capping at the minimum zoom scale specified by the scroll view
    CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
    newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
    [self.scrollView setZoomScale:newZoomScale animated:YES];
}

- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    // Return the view that you want to zoom
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    // The scroll view has zoomed, so you need to re-center the contents
    [self centerScrollViewContents];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1
    UIImage *image = [UIImage imageNamed:@"map_route1.png"];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
    [self.scrollView addSubview:self.imageView];

    // 2
    self.scrollView.contentSize = image.size;

    // 3
    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    [self.scrollView addGestureRecognizer:doubleTapRecognizer];

    UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
    twoFingerTapRecognizer.numberOfTapsRequired = 1;
    twoFingerTapRecognizer.numberOfTouchesRequired = 2;
    [self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // 4
    CGRect scrollViewFrame = self.scrollView.frame;
    CGFloat scaleWidth = scrollViewFrame.size.width / self.scrollView.contentSize.width;
    CGFloat scaleHeight = scrollViewFrame.size.height / self.scrollView.contentSize.height;
    CGFloat minScale = MIN(scaleWidth, scaleHeight);
    self.scrollView.minimumZoomScale = minScale;

    // 5
    self.scrollView.maximumZoomScale = 1.5f;
    self.scrollView.zoomScale = minScale;

    // 6
    [self centerScrollViewContents];
}

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end

因此,您可以看出每个单独地图中唯一变化的部分是这行代码:

UIImage *image = [UIImage imageNamed:@"map_route1.png"];

我想要做的是列出我所有图像的某种列表,当单击该地图(图像)的按钮时,它将用按钮也应链接的图像名称或地图替换这行代码。整个代码和一切都应该只是我的故事板中的一个活动,它链接到 MAPS 菜单中的每个按钮。

目前,所有活动都通过情节提要中的简单“控制单击拖动”链接,并包含在提供后退导航的导航控制器内。

我想用导航控制器保留后退导航,但只需替换滚动视图内的图像。

提前致谢!

-德里克

4

0 回答 0