图片的多种展示效果,及图片的拖动,缩放

1。采用第三方开源代码:iCarousel,可实现图片的多种展示效果( @"直线", @"圆圈", @"反向圆圈", @"圆桶", @"反向圆桶", @"封面展示", @"封面展示2", @"纸牌"

),另需引入<QuartzCore.framework>

2。图片的移动,拖动:UIIMageView本身不支持移动,拖动,需重写来继承UIView来实现(DragImageView)。

主要代码如下 :

1)ImageViewController.h:

#import <UIKit/UIKit.h>

#import "iCarousel.h"

@class DragImageView;

@interface  ImageViewController : UIViewController <iCarouselDataSource,iCarouselDelegate,UIActionSheetDelegate>

{

    DragImageView *image;//图片视图变量,可实现缩放,拖动

}

@property (nonatomic, retain) IBOutlet iCarousel *carousel;//与界面中的子视图UIView关联(放置在主视图底部)

@property (nonatomic, retain) IBOutlet UINavigationItem *navItem;//放置在主视图顶部,上面放置一个展示效果选择按钮


- (IBAction)switchCarouselType;//切换图片列表展示效果


@end


2)ImageViewController.m:

#import "ImageViewController.h"

#import "DragImageView.h"

#define ITEM_SPACING 200


@implementation ImageViewController


@synthesize carousel;

@synthesize navItem;


//取得沙盒目录

-(NSString *)dataFilePath {

    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    return documentsDirectory;

}

- (void)dealloc

{

    [self.carousel release];

    [self.navItem release];

    [image release];

    [super dealloc];

}

//初始加载

- (void)viewDidLoad

{

    [super viewDidLoad];

    carousel.delegate = self;

    carousel.dataSource = self;

    

    carousel.type = iCarouselTypeCoverFlow;

    navItem.title = @"封面展示";

    

    UIScrollView *scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 768, 640)];

    scrollview.pagingEnabled = YES;

    

    DragImageView *drag=[[DragImageView alloc]initWithFrame:CGRectMake(0, 0, 768, 640)];

    image=drag;

    //取程序资料数据方式:【NSBundle mainBundle

    //[image setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"0"] ofType:@"jpg"]]];

    [image setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/photo/0.jpg",[self dataFilePath]]]];

    [scrollview addSubview:image];

    [self.view addSubview:scrollview];

    [scrollview release];

}


//切换图片展示效果

- (IBAction)switchCarouselType

{

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择类型" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"直线", @"圆圈", @"反向圆圈", @"圆桶", @"反向圆桶", @"封面展示", @"封面展示2", @"纸牌", nil];

    [sheet showInView:self.view];

    [sheet release];

}

#pragma mark -


- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

{

    if (buttonIndex<0) return;

    for (UIView *view in carousel.visibleItemViews)

    {

        view.alpha = 1.0;

    }

    

    [UIView beginAnimations:nil context:nil];

    carousel.type = buttonIndex;

    [UIView commitAnimations];

    

    navItem.title = [actionSheet buttonTitleAtIndex:buttonIndex];

}


#pragma mark -

//图片列表允许显示图片的张数

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel

{

    return 20;

}


//图片列表加载每一个图片需调用此委托

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index

{

    UILabel *lbl=[[UILabel alloc]init];

    lbl.text=[NSString stringWithFormat:@"%d张图片",index];

    lbl.backgroundColor=[UIColor clearColor];

    lbl.frame=CGRectMake(0, 0, 100, 30);

    

    UIImage *imagetmp=[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/photo/%d.jpg",[self dataFilePath],index]];

    //UIImage *imagetmp=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",index] ofType:@"jpg"]];

    

    UIImage *imagesmall=[[UIImage alloc]init];

    

    imagesmall=shrinkImage(imagetmp, CGSizeMake(180, 260));


    UIView *view = [[UIImageView alloc] initWithImage:imagesmall];

    view.frame=CGRectMake(70, 80, 180, 260);

    [view addSubview:lbl];

    [imagesmall release];

    [lbl release];

    return view;

}


//图片列表动画结束调用委托

- (void)carouselDidEndScrollingAnimation:(iCarousel *)carousels{

    //此方法会将图片加载到缓存中,图片大或多时不要用,请用下面方法;

    //UIImage *imagetmp=[UIImage imageNamed:@"0.jpg"];

    //从沙盒目录中取图片(数据图片)

    [image setImage:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/photo/%d.jpg",[self dataFilePath],carousels.previousItemIndex]]];

    //从项目目录中取图片(资源图片)

    //UIImage *imagetmp=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",[carousels previousItemIndex]] ofType:@"jpg"]];

    //[image setImage:imagetmp];

}


//纸牌展示效果需调用此方法

- (CATransform3D)carousel:(iCarousel *)_carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset

{

    view.alpha = 1.0 - fminf(fmaxf(offset, 0.0), 1.0);

    

    CATransform3D transform = CATransform3DIdentity;

    transform.m34 = self.carousel.perspective;

    transform = CATransform3DRotate(transform, M_PI / 8.0, 0, 1.0, 0);

    return CATransform3DTranslate(transform, 0.0, 0.0, offset * carousel.itemWidth);

}

//生成缩略图

static UIImage *shrinkImage(UIImage *original, CGSize size) {

    CGFloat scale = [UIScreen mainScreen].scale;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    

    CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,

                                                 size.height * scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context,

                       CGRectMake(0, 0, size.width * scale, size.height * scale),

                       original.CGImage);

    CGImageRef shrunken = CGBitmapContextCreateImage(context);

    UIImage *final = [UIImage imageWithCGImage:shrunken];

    

    CGContextRelease(context);

    CGImageRelease(shrunken);

    

    return final;

}


3) DragImagView.h

#import <UIKit/UIKit.h>


@interface DragImageView : UIView

{

    CGFloat lastDistance;

CGFloat imgStartWidth;

CGFloat imgStartHeight;

    CGPoint beginPoint;

    UIImageView *imageview;

}

-(void)setImage:(UIImage *)image;

@end


4)DragImageView.m


#import "DragImageView.h"


@implementation DragImageView


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}

-(void)setImage:(UIImage *)image{

    [imageview removeFromSuperview];

    imageview=[[UIImageView alloc]initWithImage:image];

    imageview.frame=CGRectMake(100, 0, 440, 700);

    [self addSubview:imageview];

    [imageview release];

    imgStartWidth=imageview.frame.size.width;

    imgStartHeight=imageview.frame.size.height;

}

-(void)dealloc{

    [imageview release];

    [super dealloc];

}

//手指事件:1个手动为拖动,2个以上手指为缩放

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

CGPoint p1;

CGPoint p2;

CGFloat sub_x;

CGFloat sub_y;

CGFloat currentDistance;

CGRect imgFrame;

NSArray * touchesArr=[[event allTouches] allObjects];

    NSLog(@"手指个数%d",[touchesArr count]);

if ([touchesArr count]>=2) {//缩放

p1=[[touchesArr objectAtIndex:0] locationInView:self];

p2=[[touchesArr objectAtIndex:1] locationInView:self];

sub_x=p1.x-p2.x;

sub_y=p1.y-p2.y;

currentDistance=sqrtf(sub_x*sub_x+sub_y*sub_y);

if (lastDistance>0) {

imgFrame=imageview.frame;

if (currentDistance>lastDistance+2) {

NSLog(@"放大");

imgFrame.size.width+=10;

if (imgFrame.size.width>1500) {

imgFrame.size.width=1500;

}

lastDistance=currentDistance;

}

if (currentDistance<lastDistance-2) {

NSLog(@"缩小");

imgFrame.size.width-=10;

if (imgFrame.size.width<50) {

imgFrame.size.width=50;

}

lastDistance=currentDistance;

}

if (lastDistance==currentDistance) {

imgFrame.size.height=imgStartHeight*imgFrame.size.width/imgStartWidth;

                

                float addwidth=imgFrame.size.width-imageview.frame.size.width;

                float addheight=imgFrame.size.height-imageview.frame.size.height;

                

imageview.frame=CGRectMake(imgFrame.origin.x-addwidth/2.0f, imgFrame.origin.y-addheight/2.0f, imgFrame.size.width, imgFrame.size.height);

}

}else {

lastDistance=currentDistance;

}

        

}

    else//拖动

    {

        UITouch *touch = [[event touchesForView:self] anyObject];

        CGPoint point = [touch locationInView:self];

        imageview.center=CGPointMake(imageview.center.x+point.x-beginPoint.x, imageview.center.y+point.y-beginPoint.y);

        beginPoint=point;

    }

}

//手指触发开始委托事件

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    

    UITouch *touch = [[event touchesForView:self] anyObject];

    beginPoint = [touch locationInView:self];

}

//手指触发结束委托事件

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

lastDistance=0;

CGRect imgFrame;

    imgFrame=imageview.frame;

    if(imgFrame.size.width<340)//控制图片最小宽度不小于340

    {

        imgFrame=CGRectMake(imgFrame.origin.x-(128-imgFrame.size.width)/2.0f, imgFrame.origin.y-(180-imgFrame.size.height)/2.0f, 340, 700);

    }

    if(imgFrame.origin.x>100)//控制左边距不大于100

    {

        imgFrame.origin.x=100;

    }else if(imgFrame.origin.x<-100)//控制左边距不小于-100

    {

        imgFrame.origin.x=-100;

    }

    if(imgFrame.origin.y>50)//控制上边距不大于50

    {

        imgFrame.origin.y=50;

    }else if(imgFrame.origin.y<-200) {//控制上边距不小于-200

        imgFrame.origin.y=-200;

    }

    

    imageview.frame=imgFrame;

}



@end


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值