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

轩辕成天
2023-12-01

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 (nonatomicretainIBOutlet iCarousel *carousel;//与界面中的子视图UIView关联(放置在主视图底部)

@property (nonatomicretainIBOutlet 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,NSUserDomainMaskYES);

    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 allocinitWithFrame:CGRectMake(050768640)];

    scrollview.pagingEnabled = YES;

    

    DragImageView *drag=[[DragImageView alloc]initWithFrame:CGRectMake(00768640)];

    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 allocinitWithTitle:@"选择类型" delegate:self cancelButtonTitle:nildestructiveButtonTitle:nil otherButtonTitles:@"直线"@"圆圈"@"反向圆圈"@"圆桶"@"反向圆桶"@"封面展示"@"封面展示2"@"纸牌"nil];

    [sheet showInView:self.view];

    [sheet release];

}

 

 

#pragma mark -

 

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

{

    if (buttonIndex<0return;

    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(0010030);

    

    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(180260));

 

    UIView *view = [[UIImageView allocinitWithImage:imagesmall];

    view.frame=CGRectMake(7080180260);

    [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",[selfdataFilePath],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.001.00);

    return CATransform3DTranslate(transform, 0.00.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, 80, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextDrawImage(context,

                       CGRectMake(00, 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(1000440700);

    [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 allTouchesallObjects];

 

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

 

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

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

p2=[[touchesArr objectAtIndex:1locationInView: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:selfanyObject];

        CGPoint point = [touch locationInView:self];

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

        beginPoint=point;

    }

}

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

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

    

    UITouch *touch = [[event touchesForView:selfanyObject];

    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.0f340,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

 

 类似资料: