【发布时间】:2015-09-29 22:19:19
【问题描述】:
第一个问题,放轻松!
创建一个简单的 iOS 应用程序。我有一个视图,其中包含一个旋转拨盘(一个 UIImageView)(通过旋转手势旋转)和一个 UIButton,当按下它时使用 UIView animateWithDuration 将按钮移动到 3 个位置之一。
我的问题.. 旋转转盘很好,移动按钮很好,但是.. 当我移动按钮,然后旋转转盘时,按钮的位置“重置”到原来的框架。
如何停止影响按钮位置/按钮移动的旋转。
ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController <UIGestureRecognizerDelegate>{
IBOutlet UIImageView *lock;
IBOutlet UILabel *number;
IBOutlet UILabel *displayNumber1;
IBOutlet UILabel *displayNumber2;
IBOutlet UILabel *displayNumber3;
IBOutlet UIButton *slider;
AVAudioPlayer *theAudio;
}
@property (retain, nonatomic) IBOutlet UIImageView *lock;
@property (retain, nonatomic) IBOutlet UILabel *number;
@property (retain, nonatomic) IBOutlet UILabel *displayNumber1;
@property (retain, nonatomic) IBOutlet UILabel *displayNumber2;
@property (retain, nonatomic) IBOutlet UILabel *displayNumber3;
@property (retain, nonatomic) IBOutlet UIButton *slider;
@property (retain, nonatomic) AVAudioPlayer *theAudio;
-(IBAction)moveSlider:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
#define RADIANS_TO_DEGREES(radians) ((radians) * (180.0 / M_PI))
@synthesize lock, number, slider, displayNumber1, displayNumber2, displayNumber3, theAudio;
CGFloat _lastRotation;
int lastNumber;
NSTimer *clickTimer;
int _whichNumberSelected;
int _currentDirection;
CGFloat _changeDirectionMatch;
float no1Left = 61;
float no2Left = 151;
float no3Left = 238;
NSString *lastText1;
CGRect newFrameSet;
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
lock.userInteractionEnabled = YES;
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationWithGestureRecognizer:)];
rotationGestureRecognizer.delegate = self;
[lock addGestureRecognizer:rotationGestureRecognizer];
_lastRotation = 0;
lastNumber = 0;
NSString *path = [[NSBundle mainBundle] pathForResource:@"click" ofType:@"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
theAudio.volume = 1.0;
[theAudio prepareToPlay];
_currentDirection = 0;
_changeDirectionMatch = 0;
_whichNumberSelected = 1;
lastText1 = ([NSString stringWithFormat:@"0"]);
}
-(IBAction)moveSlider:(id)sender {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGRect newFrame = slider.frame;
switch (_whichNumberSelected) {
case 1:
newFrame.origin.x= no2Left;
_whichNumberSelected = 2;
break;
case 2:
newFrame.origin.x= no3Left;
_whichNumberSelected = 3;
break;
case 3:
newFrame.origin.x= no2Left;
_whichNumberSelected = 4;
break;
case 4:
newFrame.origin.x= no1Left;
_whichNumberSelected = 1;
break;
default:
break;
}
slider.frame = newFrame;
newFrameSet = newFrame;
} completion:nil];
}
-(void)handleRotationWithGestureRecognizer:(UIRotationGestureRecognizer *)rotationGestureRecognizer{
CGFloat newRot = RADIANS_TO_DEGREES(rotationGestureRecognizer.rotation) / 3.6;
lock.transform = CGAffineTransformRotate(lock.transform, rotationGestureRecognizer.rotation);
CGFloat c = _lastRotation - newRot;
_lastRotation = c;
if(c < 0){
c = c + 100;
_lastRotation = c;
}
if(c >= 100){
c = c-100;
_lastRotation = c;
}
if(c >= 99.5){
displayNumber1.text = @"0";
} else {
displayNumber1.text = ([NSString stringWithFormat:@"%.f", c]);
}
if([displayNumber1.text isEqualToString:lastText1]){
} else {
[theAudio play];
}
lastText1 = displayNumber1.text;
rotationGestureRecognizer.rotation = 0.0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
【问题讨论】:
标签: ios objective-c rotation uigesturerecognizer