我需要模拟照片库效果。
我有uiimageview
,我需要放大和缩小,缩放和 - 如果我双击(或摇动iPad) - 我希望图像恢复到实际尺寸。
我尝试了很多,但所有可用的例子都是错误的并且不完整;我不想使用uiwebview
。
有任何建议吗?
这是代码
在视图中加载 :
holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height)];
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:myimage];
imageview.contentMode = UIViewContentModeScaleAspectFit;
//holderView.contentMode = UIViewContentModeScaleAspectFit ;
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
以下是我用于函数的代码
-(void)scale:(id)sender {
[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if(zooming)
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
OldTransform = newTransform;
OldCenter = [(UIPinchGestureRecognizer*)sender view].center;
Does_Panned = YES ;
Does_transformed = YES ;
}
else {
}
}
-(void)rotate:(id)sender {
if(zooming)
{
[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
OldTransform = newTransform ;
OldCenter = [(UIPinchGestureRecognizer*)sender view].center;
Does_Panned = YES ;
Does_transformed = YES ;
}
}
-(void)move:(id)sender {
if(zooming)
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
OldCenter = translatedPoint ;
Does_Panned = YES ;
Does_transformed = NO ;
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 768) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 1024) {
finalY = 1024;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 1024) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 768) {
finalY = 1024;
}
}
[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDelay: 1.0];
[UIView setAnimationDuration:.20];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
//[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
else{
if([(UIPanGestureRecognizer*)sender maximumNumberOfTouches] == 1)
{
UIPanGestureRecognizer* recognize = (UIPanGestureRecognizer*)sender;
//change the current window width and level
if([recognize state] == UIGestureRecognizerStateBegan) {
Bigen =[recognize translationInView:recognize.view ];// [[sender view] center];
BeginX = Bigen.x;
BeginY = Bigen.y;
}
else if(
( [recognize state] == UIGestureRecognizerStateChanged )||
(recognize.state == UIGestureRecognizerStateEnded) )
{
CGPoint translation = [recognize translationInView:recognize.view ];
End = translation ;
EndX = End.x ;
EndY = End.y ;
double offsetw=0;
double offsetl=0;
if(EndX>BeginX)
{
offsetw=50*(EndX-BeginX);
}
else if(EndX<BeginX)
{
offsetw=50*(EndX-BeginX);
}
else
{
offsetw=0;
}
if(EndY>BeginY)
{
offsetl=50*(EndY-BeginY);
}
else if(EndY<BeginY)
{
offsetl=50*(EndY-BeginY);
}
else
{
offsetl=0;
}
ChangeWW_Wl(offsetw, offsetl);
[self AddImage];
}
//if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
/* CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
EndX = translatedPoint.x;
EndY = translatedPoint.y;*/
//}
}
}
}
(void)tapped:(id)sender {
if(zooming)
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
如何更新它以在摇动或双击时将图像重置为原始大小
如何过度进行不正确的变焦(第一次使用大系数变焦)
答案 0 :(得分:0)
您应该观看WWDC2010中的“使用滚动视图设计应用程序”视频,它会告诉您如何做到这一点。