iPhone:如何向左或向右旋转 UIImageview?
Posted
技术标签:
【中文标题】iPhone:如何向左或向右旋转 UIImageview?【英文标题】:iPhone: How to Rotate a UIImageview Left or Right? 【发布时间】:2010-02-04 11:01:29 【问题描述】:我想以以下方式旋转 UIImageView:如果我按住 imageview 并将手指向左移动,那么 imageview 应该向右旋转(反之亦然)。
这里是链接Balance
【问题讨论】:
【参考方案1】:我去年摆弄过这个。事实证明它比我想象的要复杂。 IIRC,这个小班做到了你想要的。它是一个 UIButtonSubclass,它显示图像并响应点击和拖动。请注意,这只是临时代码。它不做任何内存管理、清理等。
#import <UIKit/UIKit.h>
#import "BackgroundImageButton.h"
#import "WiggleImageView.h"
@interface StickerButton : UIButton
//ivars used to control selection animaiton
CGAffineTransform startTransform;
BOOL currentlyAnimating;
BOOL shouldAnimate;
//ivars to handle touches and control events
BOOL wasDrag;
BOOL wasTouchDown;
WiggleImageView * imgView;
//ivars used to control selection animaiton
@property CGAffineTransform startTransform;
@property(nonatomic, retain) WiggleImageView *imgView;
@property BOOL currentlyAnimating;
@property BOOL shouldAnimate;
//ivars to handle touches and control events
@property BOOL wasDrag;
@property BOOL wasTouchDown;
#pragma mark Initialization
-(id) initWithImage:(UIImage *)anImage atCenterPoint:(CGPoint) centerPoint;
#pragma mark Selection Animation Methods
-(void) animateSelection;
-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;
#pragma mark Self Touch Methods //not as dirty as it sounds.
-(void) touchDragSelf:(id)sender withEvent:(UIEvent *) theEvent;
-(void) touchDownSelf:(id)sender withEvent:(UIEvent *) theEvent;
-(void) touchUpSelf:(id)sender withEvent:(UIEvent *) theEvent;
#pragma mark Graphic Edit Methods
-(void) rotateRight;
-(void) rotateLeft;
-(void) scaleUp;
-(void) scaleDown;
-(void) select;
-(void) deselect;
-(void) translateMoveByX:(CGFloat) dx andY:(CGFloat) dy; //used by self to account for translated coordinates
-(void) frameMoveByX:(CGFloat) dx andY:(CGFloat) dy; //used by external to move frame in superview
@end
#import "StickerButton.h"
@implementation StickerButton
@synthesize startTransform;
@synthesize currentlyAnimating;
@synthesize shouldAnimate;
@synthesize wasDrag;
@synthesize wasTouchDown;
@synthesize imgView;
#pragma mark Initialization
- (id)initWithFrame:(CGRect)frame
if (self = [super initWithFrame:frame])
startTransform=self.transform;
currentlyAnimating=NO;
shouldAnimate=NO;
wasDrag=NO;
wasTouchDown=NO;
self.adjustsImageWhenDisabled=NO;
self.adjustsImageWhenHighlighted=NO;
self.showsTouchWhenHighlighted=NO;
[self addTarget:self action:@selector(touchDownSelf:withEvent:) forControlEvents:UIControlEventTouchDown];
[self addTarget:self action:@selector(touchDragSelf:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self addTarget:self action:@selector(touchUpSelf:withEvent:) forControlEvents:UIControlEventTouchUpInside];
return self;
-(id) initWithImage:(UIImage *)anImage atCenterPoint:(CGPoint) centerPoint
CGFloat xOrigin,yOrigin;
xOrigin=centerPoint.x-(anImage.size.width/2);
yOrigin=centerPoint.y-(anImage.size.height/2);
[self initWithFrame:CGRectMake(xOrigin, yOrigin, anImage.size.width, anImage.size.height)];
WiggleImageView *w=[[WiggleImageView alloc] initWithFrame:self.bounds];
imgView=w;
imgView.image=anImage;
[self addSubview:imgView];
return self;
//------------------------------------initWithImage:atCenterPoint:------------------------------------
#pragma mark Selection Animation Methods
-(void) animateSelectedThrob
if (!currentlyAnimating)
NSLog(@"animating");
currentlyAnimating=YES;
//startTransform=self.transform; //this has to be saved to prevent some kind of rounding error from gradually rotating the view
[UIView beginAnimations:@"selectionAnimation" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.1];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationRepeatAutoreverses:YES];
self.transform=CGAffineTransformScale(self.transform, 1.1, 1.1);
[UIView commitAnimations];
//-------------------------------------(void) animateSelectedThrob------------------------------------
-(void) animateSelection
if (!currentlyAnimating)
//NSLog(@"animating");
currentlyAnimating=YES;
startTransform=self.transform; //this has to be saved to prevent some kind of rounding error from gradually rotating the view
[UIView beginAnimations:@"selectionAnimation" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.1];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
self.transform=CGAffineTransformRotate(self.transform, (2 * M_PI / 180) );
[UIView commitAnimations];
//-------------------------------------(void) animateSelection------------------------------------
-(void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
self.transform=startTransform;
currentlyAnimating=NO;
if (shouldAnimate)
[self animateSelection];
//------------------------------------animationDidStop:finished:context:------------------------------------
#pragma mark Self Touch Methods
-(void) touchDownSelf:(id)sender withEvent:(UIEvent *) theEvent
NSLog(@"touchDownSelf");
wasTouchDown=YES;
shouldAnimate=NO;
//------------------------------------touchDownSelf:withEvent:------------------------------------
-(void) touchDragSelf:(id)sender withEvent:(UIEvent *) theEvent
NSLog(@"touchDragSelf");
if ([[theEvent touchesForView:self] count]==1)
UITouch *t=[[theEvent touchesForView:self] anyObject];
CGPoint l,p;
l=[t locationInView:self];
p=[t previousLocationInView:self];
[self translateMoveByX:(l.x-p.x) andY:(l.y-p.y)];
wasDrag=YES;
//------------------------------------touchDragSelf:withEvent:------------------------------------
-(void) touchUpSelf:(id)sender withEvent:(UIEvent *) theEvent
NSLog(@"touchUpSelf");
// if (!wasDrag && wasTouchDown)
// [self select];
//
if (wasTouchDown)
[self select];
wasDrag=NO;
wasTouchDown=NO;
//------------------------------------touchUpSelf:withEvent:------------------------------------
#pragma mark Graphic Edit Methods
-(void) rotateRight
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
[UIView setAnimationBeginsFromCurrentState:YES];
self.transform=CGAffineTransformRotate(self.transform, (M_PI / 180) );
[UIView commitAnimations];
//-------------------------------------(void) rotateRight------------------------------------
-(void) rotateLeft
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
[UIView setAnimationBeginsFromCurrentState:YES];
self.transform=CGAffineTransformRotate(self.transform, (-1*M_PI / 180) );
[UIView commitAnimations];
//-------------------------------------(void) rotateLeft------------------------------------
-(void) scaleUp
//todo add variable to track total scale so it doesn't get to big and cause problems
shouldAnimate=NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
[UIView setAnimationBeginsFromCurrentState:YES];
self.transform=CGAffineTransformScale(self.transform, 1.1, 1.1);
[UIView commitAnimations];
startTransform=self.transform;
//-------------------------------------(void) scaleUp------------------------------------
-(void) scaleDown
//todo add variable to track total scale so it doesn't get to small and cause problems
shouldAnimate=NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
[UIView setAnimationBeginsFromCurrentState:YES];
self.transform=CGAffineTransformScale(self.transform, 0.9, 0.9);
[UIView commitAnimations];
startTransform=self.transform;
//-------------------------------------(void) scaleDown------------------------------------
-(void) select
[self animateSelectedThrob];
imgView.shouldWiggle=YES;
//-------------------------------------(void) select------------------------------------
-(void) deselect
imgView.shouldWiggle=NO;
-(void) translateMoveByX:(CGFloat) dx andY:(CGFloat) dy //necessary for all points that orignate within the transformed view
NSLog(@"dx=%f,dy=%f",dx,dy);
shouldAnimate=NO;
self.transform=CGAffineTransformTranslate(self.transform, dx,dy);
startTransform=self.transform;
//------------------------------------translateMoveByX:andY:------------------------------------
-(void) frameMoveByX:(CGFloat) dx andY:(CGFloat) dy //necessary for all points that originate outside the transformed view
self.frame=CGRectMake(self.frame.origin.x+dx, self.frame.origin.y+dy, self.frame.size.width, self.frame.size.height);
//------------------------------------frameMoveByX:andY:------------------------------------
- (void)drawRect:(CGRect)rect
// Drawing code
- (void)dealloc
[super dealloc];
@end
【讨论】:
另一个自定义类。当您删除应用程序时,它旨在创建类似于跳板上的摆动效果。这与您的问题无关。你想看看 rotateRight 和 rotateLeft 方法和 self touch 方法。请注意,这些方法只是旋转视图的视觉显示,不会影响视图显示的实际图像,即如果您保存视图或将其合成到另一个视图中,您将获得图像的原始方向。 好的,现在告诉我,我正在创建一个球并将其从屏幕顶部发送到底部。为此,我拍摄了 3 张图像(如 3 个球),现在我在创建时随机选择球那么我如何检测哪个球被相交?【参考方案2】:您可以在控制器中使用 ivar 来跟踪拖动的开始。当您收到“触地”事件时,请保存起点。当您收到“触摸移动”事件时,计算从当前点到保存点的距离。两者之间的欧几里得距离可以为您提供旋转(告诉您旋转方向的符号)。适当缩放旋转(即,可能一半的视图宽度相当于 180 度),并设置图像变换的旋转。
【讨论】:
我没有任何现有的代码。我只是解释了我将如何开始解决这个问题。您应该能够毫不费力地将上面的描述翻译成代码!玩得开心... 好的,我得到了一半,但问题是 ImageView 在旋转时得到 ZigZagged。如何解决这个问题?它也只在从起点向左或向右旋转时才向左或向右旋转。我的意思是我只希望它在我将 Imageview 向左或向右移动时始终旋转。还有一个问题,您是否看过上面的链接。我想要它完全一样,请看看并指导正确的方向 图像变得“锯齿形”是什么意思?如果你想有一个累积的旋转效果,你需要添加一个 ivar 来存储之前的旋转,并将其添加到当前的旋转中,因为用户从起点拖动。您指的是哪个链接?没看到? 我已经添加了链接,请看一下以上是关于iPhone:如何向左或向右旋转 UIImageview?的主要内容,如果未能解决你的问题,请参考以下文章
如何仅在一只耳朵中播放声音 i.s.使用音频单元一次向左或向右