您的位置:首页 > 产品设计 > UI/UE

ios-UIScrollView-常用属性和方法

2014-04-24 23:11 525 查看




_uiscroolview=[[UIScrollView alloc]init];

if ([[UIDevice currentDevice].systemVersion floatValue]>6.2) {
_uiscroolview.frame=CGRectMake(0, 0, 320, 480);
}else{
_uiscroolview.frame=CGRectMake(0, 0, 320, 400);
}
_uiscroolview.contentSize=CGSizeMake(320*2, 460);//设置内容的大小
_uiscroolview.pagingEnabled=NO;//分页效果是否可用,默认为no
_uiscroolview.bounces=YES;//是否有反弹效果,默认为yes
_uiscroolview.indicatorStyle=UIScrollViewIndicatorStyleWhite;//滚动条风格
[_uiscroolview flashScrollIndicators];//一显示就显示一下滚动条,用来提示用户的
_uiscroolview.showsHorizontalScrollIndicator=YES;//显示滚动条
_uiscroolview.backgroundColor=[UIColor redColor];
_uiscroolview.contentInset=UIEdgeInsetsMake(20, 20, 50, 30);//4个参数,分别表示可以拉动的边缘扩展的大小,上,左,下,右
_uiscroolview.scrollIndicatorInsets=UIEdgeInsetsMake(20, 20, 50, 30);//同上,不过,这4个参数是用来设置滚动条的,意义相同


-(void)touches:(UIButton*) bb{
//[_uiscroolview setContentOffset:CGPointMake(320, 20) animated:YES];//在scroolview中,跳的位置,x320,y20
[_uiscroolview scrollRectToVisible:CGRectMake(320, 200, 20, 20) animated:YES];//这个是跳转到一个区域,可见就可以了,没有指定是在中间还是哪
}


//下面是一个简单得相册得放大缩小列子,原理本来一个scrollview只能放大缩小一张图,那么我们就将多个scrollview放到一个大地scrollview上

//
//  RootViewController.m
//  uiscorrview
//
//  Created by  liyang on 14-4-26.
//  Copyright (c) 2014年 liyang. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
//先是添加了一个底部scrollview,因为一个scrollview只能放大一个view
_basescrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 340, 460)];
_basescrollview.backgroundColor=[UIColor blackColor];
[self.view addSubview:_basescrollview];
//设置能容大小
_basescrollview.contentSize=CGSizeMake(340*4, 460);
_basescrollview.pagingEnabled=YES;
_basescrollview.showsVerticalScrollIndicator=NO;
_basescrollview.showsHorizontalScrollIndicator=NO;
_basescrollview.delegate=self;
_basescrollview.tag=INT_MAX;

int _x=0;
for (int index=0; index<5; index++) {
ImageScrollView *imagescrollview=[[ImageScrollView alloc]initWithFrame:CGRectMake(0+_x, 0, 320, 480)];
NSString *imagename=[NSString stringWithFormat:@"%d.jpg",index+1];
imagescrollview.tag=index;
UIImage *iamge=[UIImage imageNamed:imagename];
imagescrollview.imageview.image=iamge;
imagescrollview.backgroundColor=[UIColor purpleColor];
[_basescrollview addSubview:imagescrollview];
_x+=340;
}
}
//作用就是移动到下一张得时候改变原来得大小
int pre=0;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int current= _basescrollview.contentOffset.x/340;
ImageScrollView *nowscrollview=(ImageScrollView *)[_basescrollview viewWithTag:pre];
if (pre!=current&&nowscrollview.zoomScale>1.0) {
nowscrollview.zoomScale=1.0;
}
pre=current;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

@end


//
//  ImageScrollView.m
//  uiscorrview
//
//  Created by  liyang on 14-4-26.
//  Copyright (c) 2014年 liyang. All rights reserved.
//

#import "ImageScrollView.h"

@implementation ImageScrollView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//设置图片
_imageview=[[UIImageView alloc]initWithFrame:self.bounds];

[self addSubview:_imageview];
//设置代理
self.delegate=self;
//设置方法缩小的参数
self.maximumZoomScale=4.0;
self.minimumZoomScale=1;
self.showsHorizontalScrollIndicator=NO;
self.showsVerticalScrollIndicator=NO;
//添加了一个手势动作
UITapGestureRecognizer *taptwo=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapgesture:)];
taptwo.numberOfTapsRequired=2;
[self addGestureRecognizer:taptwo];

}
return self;
}
//手势事件
-(void)tapgesture:(UIGestureRecognizer *)gesture{

if (self.zoomScale>=4.0) {
[self setZoomScale:1.0 animated:YES];
}else{
CGPoint point= [gesture locationInView:self];
[self zoomToRect:CGRectMake(point.x-40, point.y-40, 80, 80) animated:YES];

}

}
//代理方法返回方法缩小的view的
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

return _imageview;
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: