Beginner iPhone Controller Question
I have a view that only has a button UIButton. First time clicking the butto开发者_运维知识库n will draw a square above the button and the second time will replace the square with a circle.
I need some help on writing the controller code. Please guide me to the right path. Thanks
design a shape class. Add your shapes as UIImageViews. with each button click, change the alpha on click.
@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
////////////////////////////////in your view controller class.
instantiate your class and add it as a subview.
@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
That should give you a good basic implementation idea. Essentially, you design a class and then change its shape with each click. It should alternate from square/circle/square/circle. Hope that helps.
精彩评论