UIScrollView實現迴圈滾動和自動滾動

2021-07-04 08:36:30 字數 3012 閱讀 7563

rootviewcontroller.m

@inte***ce

rootviewcontroller

()@property (nonatomic, retain) uipagecontrol         *pagecontrol;

@property (nonatomic, retain) uiscrollview          *scrollview;

@property (nonatomic, retain) nsmutablearray        *imageviewarr;

@end

@implementation rootviewcontroller

- (void)viewdidload /*

初始化pagecontrol

*/self

.pagecontrol

= [[

uipagecontrol

alloc] initwithframe:cgrectmake(

120,

480,

100,

28)]; [

self

.pagecontrol

setcurrentpageindicatortintcolor:[

uicolor

redcolor]]; [

self

.pagecontrol

setpageindicatortintcolor:[

uicolor

blackcolor]]; [

self

.pagecontrol

setnumberofpages

:[self

.imageviewarr

count

]];

[self

.pagecontrol

setcurrentpage:0];

[self

.pagecontrol

addtarget:

self

action:

@selector

(turnpage) forcontrolevents:uicontroleventvaluechanged];

[self.view

addsubview:self.pagecontrol];

[self

.pagecontrol

release];

/* 建立四個,新增到scrollerview

*/for (nsinteger i = 0; i < 4; i++) /*

取陣列最後一張,放在第0 頁

*/uiimageview *imageview1 = [[uiimageview

alloc] initwithimage:[uiimage

imagenamed:self.imageviewarr[(self.imageviewarr.count - 1)]]];

[imageview1 setframe:cgrectmake(0, 0, 320, 460)];

[self.scrollview

addsubview:imageview1];

[imageview1 release];/*

取陣列的第一張放在最後一頁

*/uiimageview *imageview2 = [[uiimageview

alloc] initwithimage:[uiimage

imagenamed:self.imageviewarr[0]]];

[imageview2 setframe:cgrectmake(320 * (self.imageviewarr.count + 1), 0, 320, 460)];

[self.scrollview

addsubview:imageview2];

[imageview2 release];

[self

.scrollview

setcontentsize:cgsizemake(

320* ([

self

.imageviewarr

count] +

2),

460)]; /*

設定初次偏移量*/[

self

.scrollview

setcontentoffset:cgpointmake(0,

0)]; /*

此方法滾動檢視內容,以便由rect定義的區域僅僅是滾動檢視內是可見的。

如果該地區已是可見的,該方法什麼都不做*/[

self

.scrollview

scrollrecttovisible:cgrectmake(

320, 0,

320,

460) animated:

no];

nslog

(@"count = %ld",

self

.imageviewarr

.count);

}#pragma mark - scrollerview的委託函式

#pragma mark - 當scrollerview滾動時觸發的方法

-(void)scrollviewdidscroll:(uiscrollview *)scrollview

#pragma mark - 當scrollerview滾動減速後停止時觸發的方法

-(void)scrollviewdidenddecelerating:(uiscrollview *)scrollviewelse

if(currentpage == ([self.imageviewarr

count] + 1))

}#pragma mark - pagecontrol選擇器方法

-(void)turnpage

#pragma mark - 定時器方法

-(void)runtimepage

- (void)didreceivememorywarning

-(void)dealloc

UIScrollView實現重用

import inte ce test19viewcell uiview 索引 property nonatomic,assign nsuinteger index 文字 property nonatomic,copy nsstring text 文字控制項 property nonatomic,r...

UIScrollView分頁的實現

uiscrollview的經典例子就是safari的編輯模式了 uiscrollview可以垂直,水平滾動。如果屬性pageenabled yes,則設定其為分頁模式,那麼沒滾動一次就是一頁,垂直方向上的一頁就是uiscrollview的高度,水平方向就是其寬度。我們可以設定contentsize,...

UIScrollView分頁的實現

uiscrollview的經典例子就是safari的編輯模式了 uiscrollview可以垂直,水平滾動。如果屬性pageenabled yes,則設定其為分頁模式,那麼沒滾動一次就是一頁,垂直方向上的一頁就是uiscrollview的高度,水平方向就是其寬度。我們可以設定contentsize,...