實現 scrollview 預設顯示指定的頁碼

2021-09-06 09:51:34 字數 2984 閱讀 6379

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

以下**執行成功

#import

@inte***ce mainviewcontroller : uiviewcontroller

@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

;@inte***ce

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

return

self;

}- (void

)viewdidload

//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

- (ibaction)changepage:(id

)sender

}- (void)loadscrollviewwithpage:(int

)page

}- (void)scrollviewdidscroll:(uiscrollview *)sender

//switch the indicator when more than 50% of the previous/next page is visible

cgfloat 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}//

at the end of scroll animation, reset the boolean used when scrolls originate from the uipagecontrol

- (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview

}-(bool)shouldautorotatetointe***ceorientation:(uiinte***ceorientation)inte***ceorientation

@end

Android scrollView預設顯示頂部

使用環境 介面太大,使用scrollview來載入布局。載入完畢之後,介面會預設顯示在scrollview的底部。使用方法 1 讓xml布局的根布局獲取焦點 就這一句2 讓scrollview在子執行緒中滑動到頂部位置 runnable runnable new runnable bhthreadp...

scrollView實現輪播

1.2.brief 建立輪播,在tableview建立之後和資料載入後 3.4.void createcarousel 24.carousel.contentoffset cgpointmake screen w,0 25.26.27.carouselback addsubview carousel...

彈性scrollview的實現

public class stretchscrollview extends scrollview public stretchscrollview context context,attributeset attrs override protected void onfinishinflate ...