日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现 scrollview 默认显示指定的页码

發布時間:2025/3/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现 scrollview 默认显示指定的页码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

思路:用scrollview的偏移量來實現

以下代碼運行成功

#import <UIKit/UIKit.h>@interface MainViewController : UIViewController<UIScrollViewDelegate> {//上面的scrollviewUIScrollView *scrollView0;UIPageControl *pageControl0; //頁面控制控件 tag 已在xib文件中設置為0NSMutableArray *arrImageViews; //相當于datasourceBOOL isLoadScrollView0; //是否加載 BOOL pageControlUsed;} @property (nonatomic,retain) IBOutlet UIScrollView *scrollView0; @property (nonatomic,retain) IBOutlet UIPageControl *pageControl0; @property BOOL isLoadScrollView0; - (IBAction)changePage:(id)sender;@end #import "MainViewController.h"static NSUInteger fNumberOfPages = 4;@interface MainViewController (PrivateMethods) - (void)loadScrollViewWithPage:(int)page; - (void)scrollViewDidScroll:(UIScrollView *)sender; @end@implementation MainViewController @synthesize scrollView0, pageControl0; @synthesize isLoadScrollView0;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization }return self; }- (void)viewDidLoad {[super viewDidLoad];/* 初始化scrollView0相關內容*///獲取scrollView0要顯示的相關內容 arrImageViews = [[NSMutableArray alloc]init];for (int i = 0; i < fNumberOfPages; i++) {UIImage *tempImage = [[UIImage alloc]init];NSString *imageName = [NSString stringWithFormat:@"pic0%d.png", i + 1];tempImage = [UIImage imageNamed:imageName];UIImageView *view = [[UIImageView alloc] initWithImage:tempImage];[arrImageViews addObject:view];}// scrollView0 初始化scrollView0.pagingEnabled = YES;scrollView0.contentSize = CGSizeMake(scrollView0.frame.size.width * fNumberOfPages, scrollView0.frame.size.height);scrollView0.showsHorizontalScrollIndicator = NO;scrollView0.showsVerticalScrollIndicator = NO;scrollView0.scrollsToTop = NO;scrollView0.delegate = self;scrollView0.tag = 1000;pageControl0.numberOfPages = fNumberOfPages;pageControl0.currentPage = 0; //這個只改變了pagecontrol 被選中的位置 isLoadScrollView0 = YES;//使用如下3句代碼 實現默認顯示 scrollview 指定的頁CGPoint pt = CGPointMake(640, 0); [scrollView0 setContentOffset:pt]; //設置scrollview 的偏移量[self scrollViewDidScroll:scrollView0]; //模擬scrollview 被滑動 }- (void)viewDidUnload {[super viewDidUnload];// Release any retained subviews of the main view.// e.g. self.myOutlet = nil; }- (IBAction)changePage:(id)sender {if ([sender tag] == 0) {int page = pageControl0.currentPage;// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)[self loadScrollViewWithPage:page - 1];[self loadScrollViewWithPage:page];[self loadScrollViewWithPage:page + 1];// update the scroll view to the appropriate pageCGRect frame = scrollView0.frame;frame.origin.x = frame.size.width * page;frame.origin.y = 0;[scrollView0 scrollRectToVisible:frame animated:YES];// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.pageControlUsed = YES;} }- (void)loadScrollViewWithPage:(int)page {if (isLoadScrollView0 == YES) {if (page < 0)return;if (page >= fNumberOfPages)return;// 獲取數據UIImageView *view = [arrImageViews objectAtIndex:page];CGRect frame = scrollView0.frame;frame.origin.x = frame.size.width * page;frame.origin.y = 0;view.frame = frame;[scrollView0 addSubview:view];} }- (void)scrollViewDidScroll:(UIScrollView *)sender {if ([sender tag] == 1000) {//設置加載的對象isLoadScrollView0 = YES;if (pageControlUsed) {return;}// Switch the indicator when more than 50% of the previous/next page is visibleCGFloat pageWidth = scrollView0.frame.size.width;NSLog(@"scrollView0.contentOffset.x === %f",scrollView0.contentOffset.x );int page = floor((scrollView0.contentOffset.x - pageWidth / 2) / pageWidth) + 1;pageControl0.currentPage = page;[self loadScrollViewWithPage:page - 1];[self loadScrollViewWithPage:page];[self loadScrollViewWithPage:page + 1];} }// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {if (scrollView.tag == 1000) {pageControlUsed = NO;} }// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {if (scrollView.tag == 1000) {pageControlUsed = NO;} }- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // return (interfaceOrientation == UIInterfaceOrientationPortrait);return NO; } @end

?

轉載于:https://www.cnblogs.com/ygm900/archive/2013/05/22/3092309.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的实现 scrollview 默认显示指定的页码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。