实现图片的滚动,并且自动停止在每张图片上
-
(void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 200)];
for (int i=1; i<=8; ++i) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320*(i-1), 0, 320, 200)];
imageView.image = image;
[scrollView addSubview:imageView];
}
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*8, 200);
[self.view addSubview:scrollView];
// Do any additional setup after loading the view.
}
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 200)];
for (int i=1; i<=8; ++i) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320*(i-1), 0, 320, 200)];
imageView.image = image;
[scrollView addSubview:imageView];
}
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*8, 200);
[self.view addSubview:scrollView];
// Do any additional setup after loading the view.
}
将图片猩猩假如scrollView 中
-
(void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 30, 300, 400)];
scrollView.backgroundColor = [UIColor orangeColor];
UIImage *image = [UIImage imageNamed:@"1.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[scrollView addSubview:imageView];
scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);
[self.view addSubview:scrollView];
// Do any additional setup after loading the view.
}
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 30, 300, 400)];
scrollView.backgroundColor = [UIColor orangeColor];
UIImage *image = [UIImage imageNamed:@"1.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[scrollView addSubview:imageView];
scrollView.contentSize = CGSizeMake(image.size.width, image.size.height);
[self.view addSubview:scrollView];
// Do any additional setup after loading the view.
}
手势:
@interface QFViewController ()<UIGestureRecognizerDelegate]]>
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
@implementation QFViewController{
CGFloat preScale;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 手势
/*
UITapGestureRecognizer;//单击
UIPinchGestureRecognizer;//缩放手势
UIRotationGestureRecognizer;//旋转手势
UISwipeGestureRecognizer;//扫动手势(上下左右)
UIPanGestureRecognizer;//滑动手势
*/
UIPinchGestureRecognizer *pinchGR=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(onPinch:)];
[self.myLabel addGestureRecognizer:pinchGR];
//label、imageview默认不接受人机交互,要手动设置
self.myLabel.userInteractionEnabled=YES;
preScale=1;
}
-(void)onPinch:(UIPinchGestureRecognizer *)pGR{
NSLog(@"%@",pGR);
NSLog(@"%f",pGR.scale);
/*
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
*/
if (pGR.state==UIGestureRecognizerStateChanged) {
self.myLabel.font=[UIFont systemFontOfSize:17*preScale*pGR.scale];
}
if (pGR.state==UIGestureRecognizerStateEnded) {
preScale=preScale*pGR.scale;
}
}
@end
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
@implementation QFViewController{
CGFloat preScale;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 手势
/*
UITapGestureRecognizer;//单击
UIPinchGestureRecognizer;//缩放手势
UIRotationGestureRecognizer;//旋转手势
UISwipeGestureRecognizer;//扫动手势(上下左右)
UIPanGestureRecognizer;//滑动手势
*/
UIPinchGestureRecognizer *pinchGR=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(onPinch:)];
[self.myLabel addGestureRecognizer:pinchGR];
//label、imageview默认不接受人机交互,要手动设置
self.myLabel.userInteractionEnabled=YES;
preScale=1;
}
-(void)onPinch:(UIPinchGestureRecognizer *)pGR{
NSLog(@"%@",pGR);
NSLog(@"%f",pGR.scale);
/*
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
*/
if (pGR.state==UIGestureRecognizerStateChanged) {
self.myLabel.font=[UIFont systemFontOfSize:17*preScale*pGR.scale];
}
if (pGR.state==UIGestureRecognizerStateEnded) {
preScale=preScale*pGR.scale;
}
}
@end
IOS UI 第八篇:基本UI