I have a view that only has a button UIButton. First time clicking the button 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.
Related
I have a custom UIView XIB that gets loaded onto my ViewController and the frame is adjusted to be collapsed before added to subview. The UIButton tied to my XIB is showing when the frame is smaller than the button location. How can I hide and show my UIButton as the frame is expanding/collapsing? My XIB is not using AutoLayout.
ViewController.m
self.greenView = [[[NSBundle mainBundle] loadNibNamed:#"Green" owner:self options:nil] objectAtIndex:0];
[self.navigationController.view addSubview:self.greenView];
GreenView.h
#interface GreenView : UIView
#property (weak, nonatomic) IBOutlet UIView *expandView;
#property (nonatomic, getter = isExpanded) BOOL expand;
#property (weak, nonatomic) IBOutlet UIButton *testButton;
- (IBAction)testButtonTapped:(id)sender;
#end
GreenView.m
#interface GreenView()
#property (nonatomic) UITapGestureRecognizer *tapGesture;
#end
#implementation GreenView
- (void)awakeFromNib {
CGRect frame = self.frame;
frame.size.height = self.expandView.frame.size.height;
frame.origin.y = 200;
self.frame = frame;
self.expand = NO;
[self.expandView addGestureRecognizer:self.tapGesture];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (UITapGestureRecognizer *)tapGesture {
if (!_tapGesture) {
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapClicked:)];
}
return _tapGesture;
}
- (void)tapClicked:(UIGestureRecognizer *)recognizer {
CGRect frame = self.frame;
self.expand = !self.expand;
frame.size.height = self.expand ? gvExpandedHeight : gvCollapsedHeight;
[UIView animateWithDuration:0.5 animations:^{
self.frame = frame;
} completion:^(BOOL finished) {
NSLog(#"Frame after animation: %#", NSStringFromCGRect(self.frame));
}];
}
- (IBAction)testButtonTapped:(id)sender {
NSLog(#"Test button tapped");
}
#end
Collapsed:
Expanded:
GreenView.xib:
GrenenView.xib Struts and Springs:
If I understood you just want to clip it, if the button is outside to the view bounds. In -awakeFromNib add:
self.clipsToBounds = YES;
Using this property you are telling the view to cut everything that is not inside its bounds, views caw draw subviews even if they are place outside. Is a little expensive by means of performance, if you use it a lot or during heavy animations.
One way around could be hide it while the view is collapsed.
You change the frame to frame.origin.y = 200;
I think you have to set the UIButton constraint relative to your GreenView in you nib file
I am a newbie to iphone development and i am doing a simple app for practice. In that app,there is only one view that has an image on it. I want to be able to swipe left or right and have the first image go out of the view and the second image to come in. The images are different and I want it to look like they are connected. I need it to be able to change the image or restart after a series of swipes. I am doing this using ScrollView only.
I have tried something:
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIScrollViewDelegate>
{
UIImageView *imgView;
UIScrollView *scrollView;
}
#property (nonatomic,retain)IBOutlet UIImageView *imgView;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
//- (id)initWithPageNumber:(int)page;
- (IBAction)changePage:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize imgView,scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
- (id)initWithPageNumber:(int)page{
if (self = [super initWithNibName:#"MyView" bundle:nil])
{
pageNumber = page;
}
return self;
}
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview
{
scrollView.contentOffset = CGPointMake(scrollview.bounds.size.width, 0);
}
- (IBAction)changePage:(id)sender
{
NSArray *images = [[NSArray alloc] initWithObjects:#"1.jpeg",#"2.jpeg",#"3.jpeg", nil];
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];
CGRect cRect = scrollView.bounds;
for (int i = 0; i < images.count; i++){
imgView = [images objectAtIndex:i];
imgView.frame = cRect;
[scrollView addSubview:imgView];
cRect.origin.x += cRect.size.width;
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * images.count, scrollView.bounds.size.height);
scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
}
#end
When i run the app, I am still not able to swipe across the images. Am i doing something wrong? Need some guidance on this. Thanks..
This error indicates that you have a connection in Interface Builder with an IBOutlet that does not exist in your view controller class.
Make sure your IBOutlets are named correctly and that you have removed all obsolete connections in IB.
scrollView.ContentSize = (scrollView.frame.size.width * images.count
,scrollView.frame.size.height)
do this after lopping it will swipe
After seeing some examples, I'm trying to create a window where an Image view could be moved with the PanGestureRecognizer. I don't understand what is missing.
I created and initialized an UIView object
I also created an UIGestureRecognizer for panning, for both views.
I created the method to be selected when Gesture is recognized.
If you have an idea of what is missing, I would be thankful to read it;
Here is my code:
View Controller.h
#import <UIKit/UIKit.h>
#import "bouton.h"
#interface ATSViewController : UIViewController {
boutonHome *bouton; }
#property (retain, nonatomic) boutonHome *bouton;
-(IBAction)handlePanGesture:(UIPanGestureRecognizer*)sender;
#end
View Controller.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"back.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
// Set self's frame to encompass the image
bouton.frame = frame;
bouton.boutonImage = image;
[self.view addSubview:bouton];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[bouton setUserInteractionEnabled:YES];
[bouton addGestureRecognizer:panGesture];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:panGesture];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender
{
CGPoint translate = [sender translationInView:self.view];
CGRect newFrame = bouton.frame;
newFrame.origin.x += translate.x;
newFrame.origin.y += translate.y;
sender.view.frame = newFrame;
if(sender.state == UIGestureRecognizerStateEnded)
bouton.frame = newFrame;
}
BoutonHome.h
#import <Foundation/Foundation.h>
#interface boutonHome : UIView
{
UIImage *boutonImage;
}
#property (nonatomic, retain) UIImage *boutonImage;
// Initializer for this object
#end
boutonHome.m
#import "bouton.h"
#implementation boutonHome
#synthesize boutonImage;
#end
So, I am trying to make a shared method in my Brain class which will check the label on a pressed button (buttons of items are in different classes) and then accordingly add a image to a ShoppingList view minding the order of adding (first image added goes to position 0,0, second to position 80,0 etc.).
I have Breakfast class which represents breakfast ingredients and I wanna add a photo of a Tea to another view called ShoppingList.
I wrote a method in Brain that adds an image to a imageView and returns an imageView which is then locally passed to the button pressed.
It builds but when I press the button Tea, application crashes.
Here is my code:
Brain.h
#interface Brain : NSObject {
#private
UIImage *image;
UIImageView *imageView;
}
#property (retain) UIImageView *imageView;
#property (retain) UIImage *image;
- (id)performOperation:(NSString *)operation;
#end
Brain.m
#implementation Brain
#synthesize imageView;
#synthesize image;
- (UIImageView *)performOperation:(NSString *)operation
{
if ([operation isEqual:#"Tea"]) {
image = [UIImage imageNamed:#"Tea_photo.jpg"];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(0, 0, 80, 80);
return imageView;
//[shoppingList.view addSubview:imageView];
//[imageView release];
}
else return 0;
}
#end
Breakfast.h
#interface Breakfast : UIViewController {
IBOutlet ShoppingList *shoppingList;
Brain *brain;
}
- (IBAction)addItemToShoppingList:(UIButton *)sender;
- (IBAction)goToShoppingList;
- (IBAction)goBack;
#end
Breakfast.m
#implementation Breakfast
- (Brain *)brain
{
if (!brain) brain = [[Brain alloc] init];
return brain;
}
- (IBAction)addItemToShoppingList:(UIButton *)sender
{
NSString *operation = [[sender titleLabel] text];
UIImageView *imageView = [[self brain] performOperation:operation];
[shoppingList.view addSubview:self.brain.imageView];
//UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
//imageView.frame = CGRectMake(0, 0, 80, 80);
//[shoppingList.view addSubview:imageView];
//[imageView release];
}
- (IBAction)goToShoppingList
{
[self presentModalViewController:shoppingList animated:NO];
}
- (IBAction)goBack
{
[self dismissModalViewControllerAnimated:NO];
}
#end
PLEASE HELP, its for my thesis.
IBOutlet ShoppingList *shoppingList;
Why make this IBOutlet? Isn't ShoppingList a class that you created. Not that this solves your crash. For that u need to post the crash log....
And also in you code I can't see any allocation for shoppingList so how can you be able to use it. You need to allocate the object in the class Brain otherwise it doesn't make any sense.
I have not gone through your code. But on the basis of your description I can suggest you to create a protocol
Without getting into OpenGL (Quartz 2D is OK):
Let's say I have an image which I want to move across a map in some fluid way. For instance, an image of a plane "flying" across the map. I've been able to do this using an MKAnnotation, an NSTimer and fiddling with the rate of lat/lon change and timer rate. However, I assume this isn't ideal, although the results look pretty decent. Can you think of a better way?
Now let's say that I want this image to be animated (think: animated gif). I can't do the usual UIImageView with a series of animationFrames because all I have access to in MKAnnotationView is a UIImage. How would you guys tackle this?
I realize that #2 could be handled with a UIImageView on top of the map containing the animationImages. However, then I would have to manually handle translating the movement of the plane or rocket or whatever as the region of the mapview changed, depending on user movements in the real-world, or user zooming (scrolling is not allowed in my app).
What do you think?
I think I've figured out a solution to #2. I subclassed MKAnnotationView and wrote some code to add a UIImageView (with animation images) as a subview.
//AnimatedAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface AnimatedAnnotation : MKAnnotationView
{
UIImageView* _imageView;
NSString *imageName;
NSString *imageExtension;
int imageCount;
float animationDuration;
}
#property (nonatomic, retain) UIImageView* imageView;
#property (nonatomic, retain) NSString* imageName;
#property (nonatomic, retain) NSString* imageExtension;
#property (nonatomic) int imageCount;
#property (nonatomic) float animationDuration;
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageName:(NSString *)name imageExtension:(NSString *)extension imageCount:(int)count animationDuration:(float)duration
;
#end
//AnimatedAnnotation.m
#import "AnimatedAnnotation.h"
#implementation AnimatedAnnotation
#synthesize imageView = _imageView;
#synthesize imageName, imageCount, imageExtension,animationDuration;
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageName:(NSString *)name imageExtension:(NSString *)extension imageCount:(int)count animationDuration:(float)duration
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
self.imageCount = count;
self.imageName = name;
self.imageExtension = extension;
self.animationDuration = duration;
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%#0.%#",name,extension]];
self.frame = CGRectMake(0, 0, image.size.width, image.size.height);
self.backgroundColor = [UIColor clearColor];
_imageView = [[UIImageView alloc] initWithFrame:self.frame];
NSMutableArray *images = [[NSMutableArray alloc] init];
for(int i = 0; i < count; i++ ){
[images addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%#%d.%#", name, i, extension]]];
}
_imageView.animationDuration = duration;
_imageView.animationImages = images;
_imageView.animationRepeatCount = 0;
[_imageView startAnimating];
[self addSubview:_imageView];
return self;
}
-(void) dealloc
{
[_imageView release];
[super dealloc];
}
#end