zoukankan      html  css  js  c++  java
  • UIscrollView网络加载滚动新闻

      这个程序是从网上下载的,我稍作了修改。用到了GCD数据加载,因为本人对block不熟,所以看起来十分纠结。等以后笨猪对block的理解更深入了,再做总结。

      基本功能是从网络下载图片,显示在UIScrollView上,每隔6秒滚动一次,我在源程序基础上做了改动,如果图片还没有加载完成,则显示很酷的风火轮,加载完成风火轮消失。

    效果图如下:

    这次用到了IB,不过很简单,拖两个控件(UIScrollView和UIPageControl)到View上,再在ViewController.h中声明这两个类型的变量,连起来。如图:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController{
    
        IBOutlet UIScrollView *scrollview;
        IBOutlet UIPageControl *page;
        NSArray *Arr;
        int TimeNum;
        BOOL Tend;
    }
    -(void)AdImg:(NSArray*)arr;
    - (void) setCurrentPage:(NSInteger)secondPage;
    - (void)createActivityView:(UIImageView*)img;
    - (void)stopActivityView:(UIImageView*)img;
    @property(retain,nonatomic)UIScrollView *scrollview;
    @property(retain,nonatomic)UIPageControl *page;
    @end

    ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize scrollview,page;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [NSTimer scheduledTimerWithTimeInterval:1 target: self selector: @selector(handleTimer:)  userInfo:nil  repeats: YES];
        Arr=[[NSArray alloc]initWithObjects:
                      @"http://www.sootuu.com/photo/uploadfile/2006-4/20064223635877.jpg",
                      @"http://www.sootuu.com/photo/uploadfile/2006-4/2006422377122.jpg",
                      @"http://pic7.nipic.com/20100604/668573_235438084702_2.jpg",nil];
        [self AdImg:Arr];
        [self setCurrentPage:page.currentPage];
        
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    #pragma mark - 6秒换图片
    - (void) handleTimer: (NSTimer *) timer
    {
        if (TimeNum % 6 == 0 ) {
            if (!Tend) {
                page.currentPage++;
                if (page.currentPage==page.numberOfPages-1) {
                    Tend=YES;
                }
            }else{
                page.currentPage--;
                if (page.currentPage==0) {
                    Tend=NO;
                }
            }
            [UIView animateWithDuration:1.0 //速度1.0秒
                             animations:^{//修改坐标
                                 self.scrollview.contentOffset = CGPointMake(page.currentPage*320,0);
                             }];        
        }
        TimeNum ++;
        NSLog(@"TimeNum==%d",TimeNum);
    }
    #pragma mark - 下载图片
    void UIImageFromURL( NSURL * URL, void (^imageBlock)(UIImage * image), void (^errorBlock)(void) )//block是一段可以在任何时候执行的代码段
    {
    //这里把imageBlock当做了一个变量,它封装了一段代码,这段代码在-(void)AdImg:(NSArray*)arr中 dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT,
    0 ), ^(void) { NSData * data = [[NSData alloc] initWithContentsOfURL:URL] ; UIImage * image = [[UIImage alloc] initWithData:data]; dispatch_async( dispatch_get_main_queue(), ^(void){//异步加载图 if( image != nil ) { imageBlock( image ); } else { errorBlock(); } }); }); } -(void)AdImg:(NSArray*)arr{ [self.scrollview setContentSize:CGSizeMake(320*[arr count], 211)]; page.numberOfPages=[arr count]; for ( int i=0; i<[arr count]; i++) { NSString *url=[arr objectAtIndex:i]; UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 211)]; img.backgroundColor = [UIColor whiteColor]; // img.tag = 1000 + i; [self createActivityView:img]; [self.scrollview addSubview:img]; UIImageFromURL([NSURL URLWithString:url], ^( UIImage * image ){ [img setImage:image]; [self stopActivityView:img]; }, ^(void){}); [img release];//红色的字体就是上一个函数imageBlock封装起来的代码段,执行的时候直接用这段代码替换void (^imageBlock)(UIImage * image) } } #pragma mark - scrollView && page - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ page.currentPage=scrollView.contentOffset.x/320;//设置当前页的偏移位置 [self setCurrentPage:page.currentPage]; } - (void) setCurrentPage:(NSInteger)secondPage { for (NSUInteger subviewIndex = 0; subviewIndex < [page.subviews count]; subviewIndex++) {//从UIPageControl的subviews中获得每张图片,再替换 UIImageView* subview = [page.subviews objectAtIndex:subviewIndex]; CGSize size; size.height = 24/2; size.width = 24/2; [subview setFrame:CGRectMake(subview.frame.origin.x, subview.frame.origin.y, size.width,size.height)]; if (subviewIndex == secondPage) [subview setImage:[UIImage imageNamed:@"a.png"]]; else [subview setImage:[UIImage imageNamed:@"d.png"]];//这里是把UIPageControl的小图标换了 } } - (void)createActivityView:(UIImageView*)img//创建风火轮 { static int size = 40; UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; activityView.frame = CGRectMake(img.frame.size.width/2 - size/2, img.frame.size.height/2 - size/2, size, size); // activityView.tag = img.tag; [img addSubview:activityView]; [activityView startAnimating]; [activityView release]; } - (void)stopActivityView:(UIImageView*)img;//移除风火轮 { // UIActivityIndicatorView *actvityIdcator = [[UIActivityIndicatorView alloc]init]; // actvityIdcator.tag = img.tag; // NSLog(@"第%d个加载完成",actvityIdcator.tag); if (img.image != nil) for (UIView *view in [img subviews]) {//读出UIButton里的所有控件,再选择UIActivityIndicatorView进行更改 if ([view isKindOfClass:[UIActivityIndicatorView class]]) { [view removeFromSuperview];//把风火轮UIActivityIndicatorView移除 } } } - (void)dealloc { [scrollview release]; [page release]; [Arr release]; [super dealloc]; } @end
  • 相关阅读:
    计算长度的方法
    自动装箱和拆箱
    基本数据包装类
    Date类
    文档参数解析
    权限修饰符
    IO流
    ArrayList集合类
    对象数组
    二维数组
  • 原文地址:https://www.cnblogs.com/xiaobaizhu/p/2824255.html
Copyright © 2011-2022 走看看