照片浏览滑动效果UIScrollView和UIPageControl组合


一个朋友去面试,让写一个用UIScrollView和UIPageControl,一激动说需要半个小时,没时间写了,浪费一次面试机会。


会写的几分钟就可以搞定了,方法其实有很多,小弟在这里献丑了,各位大牛莫要嘲笑。


刚好在学习gcd,就用gcd简单实现一下了。有错误的地方各位指点指点:


直接在入口类里面实现了,


#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate,UIScrollViewDelegate>
{
    NSMutableArray *_mutArrImg;
    UIPageControl *_pageControl;
    UIScrollView *_scrollView;
}

@property (strong, nonatomic) UIWindow *window;

@end


分别定义NSMutableArray *_mutArrImg、UIPageControl *_pageControl、UIScrollView *_scrollView全局变量。


#import "AppDelegate.h"

@implementation AppDelegate


\\这个方法,我叫它入口方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:self.window.bounds];
    [self.window addSubview:_scrollView];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(320*4, self.window.frame.size.height);
    _scrollView.pagingEnabled = YES;
    
    _mutArrImg = [NSMutableArray arrayWithCapacity:0];
    
    for (int i=0; i<4; i++) {
        NSURL *url = [NSURL URLWithString:@"http://pica.nipic.com/2007-12-12/20071212235955316_2.jpg"];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *imgData = [NSData dataWithContentsOfURL:url];
            UIImage *img = [UIImage imageWithData:imgData];
            dispatch_sync(dispatch_get_main_queue(), ^{
                [_mutArrImg addObject:img];
                UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i, 0, 320, self.window.frame.size.height)];
                imgView.image = img;
                [_scrollView addSubview:imgView];
            });
        });
    }
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 400, 320, 30)];
    _pageControl.numberOfPages = 4;
    _pageControl.currentPage = 0;
    _pageControl.pageIndicatorTintColor = [UIColor redColor];
    [self.window addSubview:_pageControl];
    [_pageControl addTarget:self action:@selector(actionCotrolPage:) forControlEvents:UIControlEventValueChanged];
    
    [self.window makeKeyAndVisible];
    return YES;
}


\\_pageControl绑定的方法,来控制_scrollView的偏移量的

- (IBAction)actionCotrolPage:(id)sender
{
    [_scrollView setContentOffset:CGPointMake(_pageControl.currentPage * 320, 0) animated:YES];
}

//​UIScrollView的代理方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    _pageControl.currentPage = _scrollView.contentOffset.x/320;
}

.

.

.


@end


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值