iOS開發 ----- UIScrollview UIPageControl

UIScrollView

    //創建滾動視圖
    //scrollViw的視圖範圍
    UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [scrollView addSubview:imageView];
    //內容的大小 默認是scrollView的大小,改變爲內容的大小,即可實現滾動
    scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);

    //隱藏滾動條
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;

    //相對於原點的偏移量
    scrollView.contentOffset = CGPointMake(image.size.width/2 - scrollView.center.x, image.size.height/2 - scrollView.center.y);

    scrollView.delegate = self;
    //自帶回彈效果,設置爲no,則取消這個效果
    scrollView.bounces = NO;


    //分頁效果,頁的寬可以自己定製
    scrollView.pagingEnabled = YES;

    //最小縮放倍數
    scrollView.minimumZoomScale = 0.5;

    //最大縮放倍數
    scrollView.maximumZoomScale = 4.0;

相關代理

#pragma mark scrollView 的代理時間


#pragma mark 整個圖片唄拖拽的過程中一直調用這個方法
-(void)scrollViewDidScroll:(nonnull UIScrollView *)scrollView
{
    NSLog(@"一直被調用 x = %f y = %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}

#pragma mark 結束減速
-(void)scrollViewDidEndDecelerating:(nonnull UIScrollView *)scrollView
{
    NSLog(@"減速的過程");
}


#pragma mark 將要減速
-(void)scrollViewWillBeginDecelerating:(nonnull UIScrollView *)scrollView
{
    NSLog(@"將要減速");
}


#pragma mark 已經結束拖拽
-(void)scrollViewDidEndDragging:(nonnull UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

}

#pragma mark 將要開始拖拽
-(void)scrollViewWillBeginDragging:(nonnull UIScrollView *)scrollView
{

}

#pragma mark 將要結束拖拽
-(void)scrollViewWillEndDragging:(nonnull UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout nonnull CGPoint *)targetContentOffset
{

}

#pragma mark 滾動到頂部 點擊狀態欄返回頂部
-(BOOL)scrollViewShouldScrollToTop:(nonnull UIScrollView *)scrollView
{
    //多個scrollView時會造成衝突,
    //一般一個設爲yes,其他爲no可以解決衝突
    //回到頂部, 可以添加刷新方法
    return YES;
}

#pragma mark 指定那個view進行縮放
-(nullable UIView *)viewForZoomingInScrollView:(nonnull UIScrollView *)scrollView
{
    return scrollView.subviews[0];
}


#pragma mark 縮放結束
-(void)scrollViewDidEndZooming:(nonnull UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale
{
    NSLog(@"縮放結束");
}

#pragma mark 縮放過程中一直調用
-(void)scrollViewDidZoom:(nonnull UIScrollView *)scrollView
{
    NSLog(@"縮放中");
}
#pragma mark 將要縮放
-(void)scrollViewWillBeginZooming:(nonnull UIScrollView *)scrollView withView:(nullable UIView *)view
{
    NSLog(@"將要縮放");
}

UIPageControl

    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40)];

    //背景顏色
    _pageControl.backgroundColor = [UIColor grayColor];

    //一共有多少個點點
    _pageControl.numberOfPages = 16;
    _index = 0;
    //當前頁
    _pageControl.currentPage = _index;
    _pageControl.pageIndicatorTintColor = [UIColor redColor]; //非當前頁的顏色
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor]; //當前的顏色

    _scrollView.delegate = self;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章