ios scrollView中增加動畫效果,自動滾動UIScrollView,利用了NSTimer

來源:互聯網
上載者:User

轉載自:http://blog.csdn.net/z343929897/article/details/7974753

按照趙總要求,在首頁上邊需要加廣告條,本來以為挺複雜的,原來挺簡單


在類的申明檔案(.h)裡添加對Page控制器的申明:

@property (strong, nonatomic) IBOutlet UIPageControl *page;@property (strong, nonatomic) IBOutlet UIScrollView *imageScrollView;
然後在實現檔案(.m)裡添加 對page對象的
@synthesize page;@synthesize imageScrollView;
實現page對象的自動存取器。 改寫viewDidLoad方法如下
- (void)viewDidLoad{    [super viewDidLoad];    //這裡定義了滾動視圖的大小,是否支援翻頁,是否顯示水平滾動標示,委派物件是哪個    imageScrollView.contentSize = CGSizeMake(PAGENUM * 320.0f, imageScrollView.frame.size.height);    imageScrollView.pagingEnabled = YES;    imageScrollView.showsHorizontalScrollIndicator = NO;    imageScrollView.delegate = self;    //這裡為滾動視圖添加了子視圖,為了能添加後續操作,我這裡定義的子視圖是按鍵UIButton    for (int i = 0; i < PAGENUM; i++) {        NSString * fileName = [NSString stringWithFormat:@"%d.jpg",i+1];        UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(i * 320.0f,  0.0f, 320.0f, 218.0f)];        [imageButton setBackgroundImage:[UIImage imageNamed:fileName] forState:UIControlStateNormal];        imageButton.tag = 900 + i;        [imageScrollView addSubview:imageButton];    }    //定義PageController 設定總頁數,當前頁,定義當控制項被使用者操作時,要觸發的動作。   �0�2page.numberOfPages = PAGENUM;    page.currentPage = 0;    [page addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];    //使用NSTimer實現定時觸發滾動控制項滾動的動作。    timeCount = 0;    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];}
增加兩個翻頁動畫和自動翻頁的函數
//滾圖的動畫效果-(void)pageTurn:(UIPageControl *)aPageControl{    int whichPage = aPageControl.currentPage;    [UIView beginAnimations:nil context:NULL];    [UIView setAnimationDuration:0.3f];    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];    [imageScrollView setContentOffset:CGPointMake(320.0f * whichPage, 0.0f) animated:YES];    [UIView commitAnimations];}//定時滾動-(void)scrollTimer{    timeCount ++;    if (timeCount == PAGENUM) {        timeCount = 0;    }
//chenyong 在這個地方加上self.pageController.currentpage = timeCount; 即可改變pageCon了,切記不要加到timeCount++的下邊
    [imageScrollView scrollRectToVisible:CGRectMake(timeCount * 320.0, 65.0, 320.0, 218.0) animated:YES];}



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.