Tuesday, June 4, 2013

UIScrollView directionalLockEnabled example in Objective C (iOS).


UIScrollView directionalLockEnabled

A Boolean value that determines whether scrolling is disabled in a particular direction

@property(nonatomic, getter=isDirectionalLockEnabled) BOOL directionalLockEnabled

Discussion of [UIScrollView directionalLockEnabled]
If this property is NO, scrolling is permitted in both horizontal and vertical directions. If this property is YES and the user begins dragging in one general direction (horizontally or vertically), the scroll view disables scrolling in the other direction. If the drag direction is diagonal, then scrolling will not be locked and the user can drag in any direction until the drag completes. The default value is NO

UIScrollView directionalLockEnabled example.
- (TappableScrollView*) createScrollView {
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    frame.origin.y = 0;

    self.scrollView = [[[TappableScrollView alloc] initWithFrame:frame] autorelease];
    scrollView.delegate = self;
    scrollView.tapDelegate = self;
    scrollView.pagingEnabled = YES;
    scrollView.alwaysBounceHorizontal = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.directionalLockEnabled = YES;
    scrollView.autoresizingMask = 0;
    scrollView.backgroundColor = [UIColor blackColor];

    frame.size.width *= posterCount;
    scrollView.contentSize = frame.size;

    return scrollView;
}

Example of [UIScrollView directionalLockEnabled].
-(void)viewDidLoad { 
    myApp = (testApp*)ofGetAppPtr(); 
 
    myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,323)]; //x,y,w,h   
    myWebView.tag = 0; // for reference purposes 
    myWebView.backgroundColor = [UIColor clearColor]; 
    myWebView.opaque = false; 
    myWebView.delegate = self; // this refers to the GuiExample view. Because of this you can put functions like webViewDidFinishLoad to the GuiExample and the webView will know where to look for it. 
    myWebView.scalesPageToFit = YES; 
 
    //also handy: I didn't want the directional lock, this is the trick to do that 
    for (id subview in myWebView.subviews) 
    {        
        if ([[subview class] isSubclassOfClass: [UIScrollView class]]){ 
            ((UIScrollView *)subview).bounces = YES; 
            ((UIScrollView *)subview).directionalLockEnabled = NO; 
        } 
    } 
     
    [self.view addSubview:myWebView]; // this adds the webview to the GuiExample 
 
}  

UIScrollView directionalLockEnabled example.
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    myscrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 44,768, 1004)];
    myscrollview.directionalLockEnabled = YES; //只能一个方向滑动 
    myscrollview.pagingEnabled = NO; //是否翻页
    myscrollview.backgroundColor = [UIColor blackColor];
    myscrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
    myscrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
    myscrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
    myscrollview.delegate = self;
    CGSize newSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+1);
    [myscrollview setContentSize:newSize];

    float x=0;
    float y=0;
    float width =768;
    float height = 1004;

    UIImageView *myimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Activity01.png"]];
    //[myimage setContentMode:UIViewContentModeScaleAspectFill];
    [myimage setFrame:CGRectMake(x, y, width, height)];
    [myscrollview addSubview:myimage];

    [self.view addSubview:myscrollview];
}

End of UIScrollView directionalLockEnabled example article.