本文介绍了如何设置UIScrollView的子类,并在Interface Builder中连接它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我在视图控制器中开始使用下面的代码,但出于对我有用的原因,我需要将以下代码放在一个单独的类中。所以我创建了一个我在下面发布的CustomView类。

First of all I began with the code below in my view controller but for reasons that work for me I needed the below code to be in a separate class. So I created a CustomView class which I've posted below.

此时我可以在视图控制器中创建此类的实例,创建一个IBOutlet并将其连接到界面构建器中的UIScrollView(或某种视图)并获得相同的行为,我将如何做这样的事情?

At this point is it possible for me to create an instance of this class in my view controller, create an IBOutlet and connect it to a UIScrollView (or some kind of view) in interface builder and get the same kind of behavior, and how would I do something like that?

customView .m

customView.m

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

@interface CustomView : UIScrollView <UIScrollViewDelegate> {

    UIScrollView    *scrollView;
    UIImageView     *imageView;

}

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

customView.m

customView.m

#import <UIKit/UIKit.h>

@implementation CustomView
@synthesize scrollView, imageView;


    - (id)init {

        if (self = [super init]) {

            // Initialization code
            UIImageView *temp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
            self.imageView = temp;
            [temp release];

            scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
            //Other Scroll View Properties
            scrollView.delegate = self;
            [scrollView addSubview:imageView];
        }

        return self;
    }


    - (void)dealloc {
        [scrollView release];
        [imageView release];
        [super dealloc];
    }


    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        //Perform an action

    }

    @end


推荐答案

您可以很容易地在IB中创建 CustomView 的实例。只需拖出 UIScrollView ,然后根据需要定位它。然后打开Identity Inspector(cmd + 4)并将类更改为 CustomView 。然后你就可以将它连接到正常的插座。

You can create an instance of CustomView in IB quite easily. Just drag out a UIScrollView and position it however you want. Then open the Identity Inspector (cmd+4) and change the class to CustomView. You can then hook this to an outlet like normal.

这篇关于如何设置UIScrollView的子类,并在Interface Builder中连接它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:19