我有一个视图,只有一个按钮UIButton。首次单击按钮将在按钮上方绘制一个正方形,第二次将用圆圈替换正方形。
我需要一些关于编写控制器代码的帮助。请引导我走正确的道路。感谢
答案 0 :(得分:0)
设计一个形状类。将您的形状添加为UIImageViews。点击每个按钮,在点击时更改alpha。
@interface Shape: UIView {
UIImageView *square;
UIimageView *circle;
BOOL isSquare;
}
@property (nonatomic, retain) UIImageView *square;
@property (nonatomic, retain) UIImageView *circle;
@property BOOL isSquare;
-(void)changeShape;
@end
@implementation Shape
@synthesize square;
@synthesize circle;
@synthesize isSquare;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
self.frame = //frame size.
//assume a square image exists.
square = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"square.png"]];
square.alpha = 1.0;
[self addSubview: square];
//assume a circle image exists.
circle = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"circle.png"]];
circle.alpha = 0.0;
[self addSubview: circle];
isSquare = YES;
}
return self;
}
-(void)changeShape {
if (isSquare == YES) {
square.alpha = 0.0;
cirle.alpha = 1.0;
isSquare = NO;
}
if (isSquare == NO) {
circle.alpha = 0.0;
square.alpha = 1.0;
isSquare = YES;
}
}
//rls memory as necessary.
@end
//////////////////////////////在你看来的控制班里。
实例化您的类并将其添加为子视图。
@class Shape;
@interface ShapeViewController : UIViewController {
Shape *shape;
}
@property (nonatomic, retain) Shape *shape;
-(IBAction)clickedButton:(id)sender;
@end
////////
#import "Shape.h"
@implementation ShapeViewController;
//add your shape as a subview in viewDidLoad:
-(IBAction)clickedButton:(id)sender {
[shape changeShape];
}
@end
那应该给你一个很好的基本实现想法。基本上,您设计一个类,然后每次单击更改其形状。它应该从正方形/圆形/方形/圆形交替。希望有所帮助。