先上效果图:

首先初始化:

- (void)viewDidLoad
{
//加入最后一张图 用于循环
int length = 4;
NSMutableArray *tempArray = [NSMutableArray array];
for (int i = 0 ; i < length; i++)
{ NSString* str = [NSString stringWithFormat:@"title%d",i];
[tempArray addObject:str];
} NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length+2]; if (length > 1)
{
NSString *str1 = [tempArray objectAtIndex:length-1]; //title3
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str1 tag:-1];
[itemArray addObject:item];
} for (int i = 0; i < length; i++)
{
NSString *str2 = [tempArray objectAtIndex:i];
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str2 tag:i];
[itemArray addObject:item]; } //加入第一张图 用于循环
if (length >1)
{
NSString *str3 = [tempArray objectAtIndex:0];
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str3 tag:length]; //title0
[itemArray addObject:item];
}
ViewFrame *bannerView = [[ViewFrame alloc] initWithFrame:CGRectMake(0, 0, 320, 105) imageItems:itemArray isAuto:NO]; [self.view addSubview:bannerView]; }
SGFocusImageItem仅仅有一个Title和tag:
@interface SGFocusImageItem : NSObject

@property (nonatomic, strong)  NSString     *title;
@property (nonatomic, assign) NSInteger tag; - (id)initWithTitle:(NSString *)title tag:(NSInteger)tag; @end

主要是ViewFrame:

#define ITEM_WIDTH 320.0
@interface ViewFrame : UIView<UIScrollViewDelegate> @property(nonatomic,strong)NSMutableArray* imageItems;
@property(nonatomic,strong)UIScrollView* scrollView; - (id)initWithFrame:(CGRect)frame imageItems:(NSArray *)items isAuto:(BOOL)isAuto;
@end
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
float targetX = _scrollView.contentOffset.x;
NSLog(@"=====scrollViewdidscroll targetX=%f",targetX); <strong> if ([_imageItems count] >= 3) {
//假设超过最后一张图,则又一次设置setContentOffset
if (targetX >= ITEM_WIDTH * ([_imageItems count]-1)) {
targetX = ITEM_WIDTH;
[_scrollView setContentOffset:CGPointMake(targetX, 0) animated:NO];
}
else if(targetX <= 0){
targetX = ITEM_WIDTH * ([_imageItems count] - 2);
[_scrollView setContentOffset:CGPointMake(targetX, 0) animated:NO];
}
}</strong>
} - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} -(void)setupViews{
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.scrollsToTop = NO; CGSize size = CGSizeMake(320, 0);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * _imageItems.count,
_scrollView.frame.size.height);
[self addSubview:_scrollView];
for (int i = 0; i < _imageItems.count; i++) {
SGFocusImageItem* item = [_imageItems objectAtIndex:i];
int x = i * _scrollView.frame.size.width;
int width = _scrollView.frame.size.width;
int height = _scrollView.frame.size.height; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, width, height)];
if (i == 0) {
imageView.backgroundColor = [UIColor brownColor];
} else if(i == 1) {
imageView.backgroundColor = [UIColor orangeColor];
} else if(i == 2) {
imageView.backgroundColor = [UIColor blueColor];
}else if(i == 3){
imageView.backgroundColor = [UIColor redColor];
}else if(i == 4){
imageView.backgroundColor = [UIColor greenColor];
}else if(i == 5) {
imageView.backgroundColor = [UIColor lightGrayColor];
} UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(x+160, 30, 50, 50)];
field.text = item.title;
// NSLog(@"====kkkkk title=%@",item.title);
field.textAlignment = UITextAlignmentCenter;
field.font = [UIFont systemFontOfSize:18]; [_scrollView addSubview:imageView];
[_scrollView addSubview:field]; } } - (id)initWithFrame:(CGRect)frame imageItems:(NSArray *)items isAuto:(BOOL)isAuto{
self = [super initWithFrame:frame];
if (self) {
_imageItems = [NSMutableArray arrayWithArray:items];
[self setupViews]; }
return self;
}

代码能够在http://download.csdn.net/detail/baidu_nod/7679089下载

版权声明:本文博主原创文章,博客,未经同意不得转载。

最新文章

  1. [Matlab] 如何在矩阵中查找特定的字符串
  2. poj3237
  3. hibernate(1)
  4. ArcEngine9.3报错Create output feature class failed
  5. uva 12304
  6. 1 Two Sum(找和为target的两个数字下标Medium)
  7. ffmpeg+SDl+ 播放器 -01
  8. WPF学习(12)动画
  9. MyBatis中update的使用
  10. 无分类编址 CIDR (构成超网)
  11. 跨站请求伪造(CSRF)-简述
  12. es crul查询(一)
  13. [转]windows BLE编程 net winform 连接蓝牙4.0
  14. Spark源码剖析 - 计算引擎
  15. ubuntu 应用添加进环境变量
  16. 菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表ngx_chain_t
  17. mac item2 ssh
  18. JavaScript -- 标签 , Break 和 Continue 语句
  19. Weird journey CodeForces - 788B (路径计数)
  20. app.jsNodejs启动测试服务

热门文章

  1. SpringBoot学习:获取yml和properties配置文件的内容(转)
  2. mysql数据库各存储引擎比較
  3. IOS自动化测试 UIAutomation
  4. [WASM] Write to WebAssembly Memory from JavaScript
  5. cocos2d-x 3.0 android mk文件 之 自己主动遍历*.cpp文件
  6. GTK入门学习:glade的使用
  7. C API函数描写叙述(S-W)
  8. Android5.0(Lollipop) BLE蓝牙4.0+浅析code(二)
  9. stm32的APB1和APB2时钟
  10. [Angular2Fire] Firebase auth (Google, Github)