尝试使用6.3.2 objective-C在Xcode中旋转按钮

时间:2015-06-10 18:33:34

标签: xcode6

我观看了一个关于如何在Objective-C中旋转按钮的视频。我完全按照视频输入了所有代码,除了我得到了一些syntex错误/“!”。我是Xcode的新手,只知道javascript的一些基础知识。请帮忙一直试图解决这个问题太久了。

这是我在ViewController.h中所做的:

 //  ViewController.h


#import <UIKit/UIKit.h>

#import <QuartzCore/QuartzCore.h>

#import <AVFoundation/AVFoundation.h>

这段代码我得到“!”,它似乎不喜欢“()”。当我拿出那部分它没关系但是稍后在ViewController.m中调用它时似乎没有识别出按钮。我会解释每个“!”说写代码,我得到每个错误。将它与代码插入错误的2“//”旁边。

@interface ViewController : UIViewController( // ! Method type specifier must start with '-' or '+'

我在下一行代码中收到3条错误消息。

IBOutlet UIButton *button; //! IBOutlet attribute ignored when parsing type, Expected selector for Objective-C method, and Expected ')' 
) //! Expected identifier or '('                                             

- (IBAction)spin:(id)sender;


@end


Now here's what I did in ViewController.m


//  ViewController.m



#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)spin:(id)sender {

CABasicAnimation *fullRotation = [CABasicAnimation 

animationWithKeyPath:@"transform.rotate" ];

fullRotation.fromValue = [NSNumber numberWithFloat:0];

fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];

fullRotation.duration = 2.0;

fullRotation.repeatCount = 300;

 in this part of the code I get the "!" again no matter what I do to change it 

[button.layer addAnimation:fullRotation forKey:@"360"]; //! Use of undeclared identifier 'button'



}
@end

2 个答案:

答案 0 :(得分:0)

UIViewController(最后不应该有(

答案 1 :(得分:0)

只需使用:

//  ViewController.h
@interface ViewController : UIViewController

@property(nonatomic, weak)  UIButton *button;
- (IBAction)spin:(id)sender;

建议使用@property,因为它会生成getter / setter,你可以设置弱引用。

并在实现文件中添加self。

[self.button.layer addAnimation:fullRotation forKey:@"360"]