IOS:实现对图片的双击放大、移动、捏合操作
来源:互联网 发布:淘宝二心级怎么升级3心 编辑:程序博客网 时间:2024/04/30 05:20
//
// EditPhotoView.m
// FaceStar
//
// Created by Monster on 12-12-21.
// Copyright (c) 2012年 FaceStar. All rights reserved.
//
#import "EditPhotoView.h"
#import "IconSelectViewController.h"
#define kScreenWidth 768
#define kScreenHeight 1024
#define kMaxZoom 3.0
@implementation EditPhotoView;
@synthesize imgView = _imgView;
@synthesize url = _url;
@synthesize width = _width;
@synthesize height = _height;
@synthesize activityIndicatorView = _activityIndicatorView;
@synthesize isTwiceTaping = _isTwiceTaping;
@synthesize scrollView = _scrollView;
@synthesize currentScale = _currentScale;
@synthesize isDoubleTapingForZoom = _isDoubleTapingForZoom;
@synthesize touchX = _touchX;
@synthesize touchY = _touchY;
@synthesize offsetY = _offsetY;
@synthesize aImage = _aImage;
#if 1
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self)
{
// Initialization code
//_imgView = [[UIImageView alloc]initWithImage:[imageDelegate getImge]];
_width =768;
_height =1024;
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[selfdraw];
}
- (void) draw
{
//self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
self.backgroundColor = [UIColorblackColor];
self.alpha =0.0;
_scrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,kScreenWidth,kScreenHeight)];
[selfaddSubview:_scrollView];
//_scrollView.delegate = self;
_scrollView.maximumZoomScale =5.0;
CGFloat ratio =_width/_height*kScreenHeight/kScreenWidth;
CGFloat min =MIN(ratio,1.0);
_scrollView.minimumZoomScale = min;
CGFloat height =_height /_width *kScreenWidth;
_imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(_scrollView.contentOffset.x+100,_scrollView.contentOffset.y+230,10,10)];
[_imgView initWithImage:_aImage];
//[_imgView initWithImage:[UIImage imageNamed:@"LoginBg.png"]];
//UIImage* img = [imageDelegate getImage];
//NSLog(@"%@",img);
//[_imgView initWithImage:[imageDelegate getImage]];
//NSLog(@"%@",[imageDelegate getImge]);
//[_imgView loadImage:_url];
CGFloat y = (kScreenHeight - height)/2.0;
_offsetY =0.0-y;
_scrollView.contentSize =CGSizeMake(kScreenWidth,kScreenHeight);
[_scrollView addSubview:_imgView];
[_imgViewrelease];
_scrollView.contentOffset =CGPointMake(0,0.0-y);
[UIViewanimateWithDuration:0.6
delay:0.0
options:UIViewAnimationCurveEaseOut
animations:^{
_imgView.frame =CGRectMake(0,0,kScreenWidth, height);
self.alpha =1.0;
}
completion:^(BOOL finished){
}
];
UITapGestureRecognizer *tapImgView = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapImgViewHandle)];
tapImgView.numberOfTapsRequired =1;
tapImgView.numberOfTouchesRequired =1;
[selfaddGestureRecognizer:tapImgView];
UITapGestureRecognizer *tapImgViewTwice = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapImgViewHandleTwice:)];
tapImgViewTwice.numberOfTapsRequired =2;
tapImgViewTwice.numberOfTouchesRequired =1;
[selfaddGestureRecognizer:tapImgViewTwice];
[tapImgViewrequireGestureRecognizerToFail:tapImgViewTwice];
[tapImgViewrelease];
[tapImgViewTwicerelease];
//UITapGestureRecognizer *saveTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(saveTapHandler)];
//UIButton *save = [UIButton generateImage:@"save-pic-button.png" hover:@"save-pic-button-hover.png" withX:245 withY:420];
//[save addGestureRecognizer:saveTap];
//[self addSubview:save];
//_activityIndicatorView = [commonFunctions generateActivityIndicatorView];
//[self addSubview:_activityIndicatorView];
}
#pragma mark - UIscrollViewDelegate zoom
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
_currentScale = scale;
NSLog(@"current scale:%f",scale);
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return_imgView;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
//当捏或移动时,需要对center重新定义以达到正确显示未知
CGFloat xcenter = scrollView.center.x,ycenter = scrollView.center.y;
NSLog(@"adjust position,x:%f,y:%f",xcenter,ycenter);
xcenter = scrollView.contentSize.width > scrollView.frame.size.width?scrollView.contentSize.width/2 :xcenter;
ycenter = scrollView.contentSize.height > scrollView.frame.size.height ?scrollView.contentSize.height/2 : ycenter;
//双击放大时,图片不能越界,否则会出现空白。因此需要对边界值进行限制。
if(_isDoubleTapingForZoom)
{
NSLog(@"taping center");
xcenter =kMaxZoom*(kScreenWidth -_touchX);
ycenter =kMaxZoom*(kScreenHeight -_touchY);
if(xcenter > (kMaxZoom -0.5)*kScreenWidth){//放大后左边超界
xcenter = (kMaxZoom -0.5)*kScreenWidth;
}elseif(xcenter <0.5*kScreenWidth){//放大后右边超界
xcenter =0.5*kScreenWidth;
}
if(ycenter > (kMaxZoom -0.5)*kScreenHeight){//放大后左边超界
ycenter = (kMaxZoom -0.5)*kScreenHeight +_offsetY*kMaxZoom;
}elseif(ycenter <0.5*kScreenHeight){//放大后右边超界
ycenter =0.5*kScreenHeight +_offsetY*kMaxZoom;
}
NSLog(@"adjust postion sucess, x:%f,y:%f",xcenter,ycenter);
}
[_imgViewsetCenter:CGPointMake(xcenter, ycenter)];
}
#pragma mark - tap
-(void)tapImgViewHandle
{
return;
NSLog(@"%d",_isTwiceTaping);
if(_isTwiceTaping){
return;
}
NSLog(@"tap once");
[UIViewanimateWithDuration:0.6
delay:0.0
options:UIViewAnimationCurveEaseOut
animations:^{
_imgView.frame =CGRectMake(_scrollView.contentOffset.x+100,_scrollView.contentOffset.y+230,10,10);
self.alpha =0.0;
}
completion:^(BOOL finished){
[selfremoveFromSuperview];
}
];
}
-(IBAction)tapImgViewHandleTwice:(UIGestureRecognizer *)sender
{
_touchX = [senderlocationInView:sender.view].x;
_touchY = [senderlocationInView:sender.view].y;
if(_isTwiceTaping)
{
return;
}
_isTwiceTaping =YES;
NSLog(@"tap twice");
if(_currentScale >1.0)
{
_currentScale =1.0;
[_scrollViewsetZoomScale:1.0animated:YES];
}else
{
_isDoubleTapingForZoom =YES;
_currentScale =kMaxZoom;
[_scrollViewsetZoomScale:kMaxZoomanimated:YES];
}
_isDoubleTapingForZoom =NO;
//延时做标记判断,使用户点击3次时的单击效果不生效。
[selfperformSelector:@selector(twiceTaping)withObject:nilafterDelay:0.65];
NSLog(@"sdfdf");
}
-(void)twiceTaping
{
NSLog(@"no");
_isTwiceTaping =NO;
}
-(void) saveTapHandler
{
if([_activityIndicatorViewisAnimating])
{
return;
}
[_activityIndicatorViewstartAnimating] ;
UIImageWriteToSavedPhotosAlbum(_imgView.image,self,@selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:),nil);
}
#pragma mark - savePhotoAlbumDelegate
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *) contextInfo
{
NSString *message;
NSString *title;
[_activityIndicatorViewstopAnimating];
if (!error)
{
title =@"恭喜";
message =@"成功保存到相册";
}else
{
title =@"失败";
message = [errordescription];
}
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:titlemessage:messagedelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil];
[alertshow];
[alertrelease];
}
#endif
-(void)dealloc
{
[superdealloc];
[_scrollView release];
}
@end
- IOS:实现对图片的双击放大、移动、捏合操作
- iOS-实现图片的捏合放大与缩小
- ios UIWebView捏合放大缩小的实现
- UIScrollView下对图片捏合放大缩小和双击放大缩小
- UIScrollView下对图片捏合放大缩小和双击放大缩小
- ios 双指捏合放大缩小图片的例子
- ios 双指捏合放大缩小图片的例子
- 捏合视图变化(视图的缩放), 单击随着鼠标移动, 双击放大视图,再双击还原视图
- 图片双击放大,可以移动!
- iOS 图片捏合放大缩小 点击放大缩小
- 双击放大图片 手指移动图片位置
- photoview实现图片双击放大缩小平移操作
- iOS 滑动浏览图片 捏合缩小放大 音量线控
- android实现对图片的放大缩小旋转等操作
- iOS中单双击,滑动和捏合手势的实现原理
- 手势状态控制图片的缩放和移动(双击放大,俩指滑动图片缩放)
- vb2005对图片放大方法的实现
- WriteableBitmap实现对图片的放大缩小
- 项目管理心得
- HDOJ 【C】 2088 Box of Bricks
- 暮光若茶色,流年落倾城。
- hdu 1271 简单并查集
- ActiveX , NPAPI 如何回调js代码
- IOS:实现对图片的双击放大、移动、捏合操作
- layout_weight
- Delphi的学习笔记十一——类1
- u盘不能格式化怎么办
- String,StringBuffer与StringBuilder的区别
- 设置SQL Server 2008 以允许远程连接
- Mysql安装
- 在object-c运行时替换私有类的方法
- java十大人物