1.UIImageView概述及初始化方法
UIImageView继承于UIView,主要用于在界面上现实图片,图片可以自动适应UIImageView的宽度和高度,而不用修改 分辨率(UIButton也可以自适应大小)
1.1初始化方法:
(1)- (id)initWithImage:(UIImage *)p_w_picpath;
- (id)initWithImage:(UIImage *)p_w_picpath highlightedImage:(UIImage *)highlightedImage
(2)也可使用父类初始化方法
-(id)initWithFrame:(CGRect)frame;
示例:
//创建一个图像对象
NSString *imgFilePath = [[NSBundle mainBundle] pathForResource:@"pictrue" ofType:@"jpg"];
UIImage *img = [UIImagep_w_picpathWithContentsOfFile:imgFilePath];
//或者以下对象方法
// UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgFilePath];
UIImageView *p_w_picpathView = [[UIImageViewalloc] initWithImage:img];
1.2 UIImageView常用属性
p_w_picpath //设置图片
userInteractionEnabled //用户交互,默认为NO,如果要触发点击事件,需要改为YES(例如添加手势)
animationImages //设置一组动态图片
animationDuration //设置播放一次一组动态图片的时间
animationRepeatCount //设置重复次数startAnimating //开始动画
startAnimating //开始动画
stopAnimating //结束动画
2.手势与UIImageView综合示例:
UIImageView *p_w_picpathView = [[UIImageView alloc] initWithImage:[UIImage p_w_picpathNamed:@"狗狗.jpg"]]; p_w_picpathView.frame = CGRectMake(50, 50, 100, 100); p_w_picpathView.center = CGPointMake(160, 240); [self.view addSubview:p_w_picpathView]; //打开p_w_picpathView视图交互,很重要,否则点击图片没反应 p_w_picpathView.userInteractionEnabled = YES; self.view = p_w_picpathView; UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(doPinch:)]; [p_w_picpathView addGestureRecognizer:pinchGestureRecognizer]; - (void)doPinch:(UIPinchGestureRecognizer *)pinchGesture { NSLog(@"捏合了"); pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale); pinchGesture.scale = 1; }
其他手势方法的重写:
- (void)dotap:(UITapGestureRecognizer *)tapGesture
{
// tapGesture.view.backgroundColor = [UIColor redColor];
UIImageView *p_w_picpathView = (UIImageView *)tapGesture.view;
// p_w_picpathView.p_w_picpath = [UIImage p_w_picpathNamed:@"猫猫.jpg"];
p_w_picpathView.backgroundColor = [UIColor yellowColor];
}
- (void)doRotation:(UIRotationGestureRecognizer *) rotationGesture
{
NSLog(@"旋转");
rotationGesture.view.transform = CGAffineTransformRotate(rotationGesture.view.transform, rotationGesture.rotation);
rotationGesture.rotation = 1;
}
- (void)doPan:(UIPanGestureRecognizer *)panGesture
{
CGPoint point = [panGesture translationInView:panGesture.view];
NSLog(@"%f ,%f",point.x,point.y);
// 此方法不完美
// panGesture.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
panGesture.view.transform = CGAffineTransformTranslate(panGesture.view.transform, point.x, point.y);
[panGesture setTranslation:CGPointZero inView:panGesture.view];
}
3.UIImageView动画与UISlider综合练习(详情见附件)
代码示例:
//初始化Uislider,并设置其参数
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(30, 300, 250, 30)];
slider.value = 10;
slider.maximumValue = 0.5;
slider.minimumValue = 0.1;
[slider addTarget:self action:@selector(doSlider:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview: slider];
//初始化UIImageView
_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 80, 80)];
NSMutableArray * arr = [NSMutableArray arrayWithCapacity:9];
for (int i = 2; i<11; i++)
{
NSString *imgName = [NSString stringWithFormat:@"1-%d(被拖移).tiff",i];
UIImage *p_w_picpath = [UIImage p_w_picpathNamed:imgName];
[arr addObject:p_w_picpath];
}
//添加UIImageView动态图片
_imgView.animationImages = arr;
//UIImageView重复次数
_imgView.animationRepeatCount = 5;
//UIImageView播放一次一组动态图片的时间
_imgView.animationDuration = slider.value;
[_imgView startAnimating];
[self.view addSubview:_imgView];
//slider方法的实现部分
- (void)doSlider:(UISlider *)slider
{
//先停止动画播放
[_imgView stopAnimating];
//词句很关键,设置播放时间等于slider.value
_imgView.animationDuration = slider.value;
[_imgView startAnimating];
}
转载于:https://blog.51cto.com/7300377/1403729