Connecting a UIScrollView to a UIView class

278 views Asked by At

I'm currently creating a "slide-show" of pictures that the user can scroll through. Following a guide, I made it so that the UIScrollView I am using shows the edges of the previous and next pictures as the user scrolls along. This comes with a side-effect of the user not being able to scroll if he touches on one of the edges of the pictures, because these edges are technically not within the border of the UIScrollView itself. In order to compensate for this, I am going to create a UIView in which I will embed the UIScrollView. The UIVew will be extend the entire width of the page so that the user can scroll when touching the edges of the pictures. To do this, I need to connect the UIScrollView to the code of the UIView through an IBOutlet. Usually this is simply accomplished by Ctrl-clicking on the UIScrollView and dragging to the code. However, this does not seem to work for me and I am stumped as to why.

Here is a screenshot of the environment I am dealing with when I try to ctrl-click on the UIScrollView and drag to the code to create an IBOutlet (it simply doesn't give the option to create anything).

enter image description here

Here is a screenshot of what running the simulator produces. If i try to click and drag where my mouse currently is, it doesn't scroll, which is the problem I am trying to correct.

enter image description here

2

There are 2 answers

1
Özgür Ersil On

It is because you should link your storyboard view to UIView class. You can choose your ScrollViewController class in custom class settings. I added the sample jpgenter image description here

0
Nils Ziehn On

what you are looking for is not that 'easy' by just connecting the two.

There are two options:

1) In the future you should use a UICollectionView and implement the paging behaviour yourself, that is the cleanest code I think.

NEVER MIND THE SECOND OPTION - I DIDN'T SEE YOU ACTUALLY ALREADY USED THIS 2) To directly answer your question:

You have to subclass the UIView that contains the UIScrollView like this:

header:

#import <UIKit/UIKit.h>

@interface SCTouchForwardView : UIView

@property (nonatomic, weak) IBOutlet UIView* receiver;
@property (nonatomic, assign) BOOL force;

@end

Implementation:

#import "SCTouchForwardView.h"

@implementation SCTouchForwardView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];
    if (![result isKindOfClass:[SCTouchForwardView class]]) {
        return result;
    }
    if (result==self) {
        UIView *result = [self.receiver hitTest:[self convertPoint:point toView:self.receiver] withEvent:event];
        if (result) {
            return result;
        }else{
            if (self.force) {
                return self.receiver;
            }
            return nil;
        }
    }
    return nil;
}

@end

When you want to use this, you just have to Right-Click-Drag from the container to the UIScrollview and select 'receiver' and what you tried to do will work!