UIScrollView 1 代码保存

本文介绍了一个自定义的 iOS 图片滑动视图组件,该组件使用 UIScrollView 实现了图片的水平滑动功能,并集成了 UIPageControl 用于指示当前页面。此外,还包含了简单的工具栏和按钮。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import <UIKit/UIKit.h>

@interface DetailView : UIView <UIScrollViewDelegate>

@property(nonatomic,retain)UIView *upView;
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)UIPageControl *pageControl;
@end



#import "DetailView.h"
@implementation DetailView
@synthesize upView;
@synthesize scrollView;
@synthesize pageControl;
const CGFloat kScrollHeight = 320; //图片的高度与宽度
const CGFloat kScrollWidth = 300;
const CGFloat imagePreView = 3; //每页显示图片数量
const NSInteger numberOfImage = 4; //总展示图片数
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self buildView];
    }
    return self;
}

- (void)buildView
{
    self.backgroundColor = [UIColor orangeColor];
    //上部工具条
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backBtn setFrame:CGRectMake(20, 10, 40, 25)];
    [closeBtn setBackgroundImage:[UIImage imageNamed:@"close.png"] forState:UIControlStateNormal];
    [closeBtn setFrame:CGRectMake(880, 10, 25, 25)];
    [backBtn addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    [closeBtn addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside];
    upView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 920, 40)];
    upView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"upvie.png"]];
    [upView addSubview:backBtn];
    [upView addSubview:closeBtn];
    [self addSubview:upView];
    
    //scrollView 滑动
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(15, 50, kScrollWidth*imagePreView-10, kScrollHeight)];
    scrollView.delegate = self;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor purpleColor];
    [scrollView setPagingEnabled:YES];
    [self scrollViewCycle];
    [self addSubview:scrollView];

    //翻页 pageControll
    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(410, 450, 100, 50)];
    pageControl.userInteractionEnabled = YES;
    pageControl.numberOfPages = 4;
    pageControl.currentPage = 0;
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:pageControl];
    pageControl.currentPage = 0;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int index = fabs(self.scrollView.contentOffset.x)/self.scrollView.frame.size.width;
    pageControl.currentPage = index;
    //int index = fabs(self.scrollView.contentOffset.x)/self.scrollView.frame.size.width;
    
}
- (void)scrollViewCycle
{
    for (int i = 1; i <= imagePreView*4; i++) {
        int d = i%4 +1;
        NSString *imageName = [NSString stringWithFormat:@"%d.jpg",d];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.frame = CGRectMake(0, 0, kScrollWidth-10, kScrollHeight);
        imageView.tag = i;
        [scrollView addSubview:imageView];
    }
    UIImageView *imageV = nil;
    NSArray *subviews = [scrollView subviews];
    CGFloat curXLoc = 0;
    for(imageV in subviews){
        if ([imageV isKindOfClass:[UIImageView class]] && imageV.tag > 0 ) {
                CGRect frame = imageV.frame;
                frame.origin = CGPointMake(curXLoc, 0);
                imageV.frame = frame;
                curXLoc += kScrollWidth;
        }
     [scrollView setContentSize:CGSizeMake((imagePreView*4* kScrollWidth), [scrollView bounds].size.height)];
    }
}
- (void)changePage:(id)sender {
    int page = pageControl.currentPage;
    [scrollView setContentOffset:CGPointMake(kScrollWidth * page *3, 0)];
}
- (void)backBtnPressed
{
    self.hidden = YES;
}

@end




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值