How to use CGAffineTransformMakeRotation for NSString - iphone

I want to use CGAffineTransformMakeRotation for NSString, how can i do it? Its working with labels and i need for my function only strings.

CGAffineTransformMakeRotation is used to set the transform property of a UIView, NSString is not a UIView so it cant be transformed, it has not concept of rotation
you need to do this at display time with a UILabel
an example:
NSString *myString = #"whatever";
CGAffineTransform stringTransform = CGAffineTransformMakeRotation(M_PI / 2.0f);
UILabel *stringLabel = [[UILabel alloc] initWithFrame:labelFrame];
stringLabel.transform = stringTransform;
stringLabel.text = myString;
[self.view addSubview:stringLabel];

You can rotate text label like below code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
label.text = #"fof fof fof";
[self.view addSubview:label];
}
- (IBAction)rotate:(id)sender {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(endAnimation)];
CGAffineTransform rotate = CGAffineTransformMakeRotation(90.0f * (M_PI / 180.0f));
label.transform = rotate;
[UIView commitAnimations];
}

Related

How to move an array of images randomly in iphone app

How can I move an array of images randomly in iPhone app.
Bellow code is for moving one image. I've to use an array of images.
- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *imageToMove =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"fish.png"]];
imageToMove.frame = CGRectMake(70, 120, 100, 100);
[self.view addSubview:imageToMove];
[self moveImage:imageToMove duration:1.0
curve:UIViewAnimationCurveLinear x:0.0 y:110.0];
}
you can use this it might help you:
NSArray *animationArray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"img1.png"],
[UIImage imageNamed:#"img2.png"],
[UIImage imageNamed:#"img3.png"],
[UIImage imageNamed:#"img4.png"],
nil];
UIImageView *animationView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 460)];
animationView.backgroundColor=[UIColor purpleColor];
animationView.animationImages=animationArray;
animationView.animationDuration=1.5;
animationView.animationRepeatCount=0;
[animationView startAnimating];
[self.view addSubview:animationView];
[animationView release];
This is just a conceptual code so please make changes according to your need.

How to animate view from top to bottom?

In my app I want to launch UIViewController from top to bottom.So from FirstViewController I am launching MyWebViewController(which I need to animate) as follows:
MyWebViewController *theWebViewController = [[MyWebViewController alloc] initWithFrame:self.view.bounds];
[self.view addSubview:theWebViewController.view];
and In loadView of MyWebViewController I am assigning frame to view as follows;
- (void)loadView
{
CGRect theFrame;
theFrame = CGRectMake(0.0, 20.0, mFrame.size.width, 0.0);
UIView *view = [[UIView alloc] init];
view.frame = theFrame;
self.view = view;
[view release];
}
and In viewDidLoad I am changing the frame as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
[UIView beginAnimations:#"animateView" context: nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
self.view.frame = CGRectMake(0.0, 20.0, mFrame.size.width, mFrame.size.height-20.0);
[UIView commitAnimations];
}
But the animation is not happening.I even tried this code to animate from bottom to top and it is working fine but vice-versa not working.Please can anyone tell me where I am doing wrong and how I can achieve?
Thanks in advance!
I wouldn't think to set the height to 0. Instead:
- (void)loadView
{
CGRect theFrame;
theFrame = CGRectMake(0.0, -mframe.size.height + 20, mFrame.size.width, m.frame.size.height - 20);
UIView *view = [[UIView alloc] init];
view.frame = theFrame;
self.view = view;
[view release];
}
You want to set the frame above and out of the view when you add it. Then move it into place in the animation.

animate and resize UIImageView

How do I animate and resize a UIImageView? I tried doing this and it didn't work out for me:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
imgView.frame = CGRectMake(0, 0, 320, 480);
[UIView commitAnimations];
Try using a block animation:
[UIView animateWithDuration:1.0f // This can be changed.
animations:^{
CGRect frame = imageView.frame;
frame.size.width += 100.0f; // This is just for the width, but you can change the origin and the height as well.
imageView.frame = frame;
}
completion:^(BOOL finished){
}];
Hope that Helps!
Try this code. This works fine for me,
CGPoint newLogoutCenter = CGPointMake(160, 364);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
twitterBarVIew.center = newLogoutCenter;
[UIView commitAnimations];
If we assume that you have correct reference to the imgView, then it seems that starting frame of the imgView is the same before and after animation.
Try to set imgView.frame to:
imgView.frame = CGRectMake(100,100,100,100);
//Set up the UIImageView. the Frame determines the position and size of the image.
UIImageView *i = [[UIImageView alloc] initWithFrame:[CGRectMake(50.0,50.0,50.0,50.0)]];
//Add images to UIImageView
i.images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"], nil];
//Set the amount of time for the animation to run
i.animationDuration = 1.0f;
//start the animations
[i startAnimating];
You can resize the image by changing the frame of the image
i.frame = CGRectMake(x, y, width, height);

How to specify a size to a UIAlert

I am trying to display a UIImage in a UIAlert.
I want to display it nearly fullscreen. For example my image has a 300x450px size.
Well I add it as a subview to my UIAlert.
But the UIAlert has default coordinates to keep it centered.
So I can add a image bigger than the UIAlert frame, but it covers the UIAlert...
I tried to specify a frame for my UIAlert, but it has no effect on it.
In fact I would like to add my image as a real content of the UIAlert, as well as simple text.
Is this possible ?
Consider writing your own Dialog popup instead of messing with the UIAlertView. If you want the 'bounce-in' animation that can easily be achieved using transform animations. Here is a simple popup dialog box I've just made.
To test it out create a new View-Based Application project and add this class. Then you can test it by adding the 'usage' code below to your *ViewController.m
This is a very simple example to demo the theory. It would make more sense to have a static method in SimpleDialog that shows the popup, but I didn't want to make the example overly complex.
SimpleDialog.h
#import <UIKit/UIKit.h>
#interface SimpleDialog : UIView
{
}
- (void) show;
#end
SimpleDialog.m
#import "SimpleDialog.h"
#define BOUNCE_SPEED 1
#implementation SimpleDialog
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
self.backgroundColor = [UIColor greenColor];
UIButton* closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
closeButton.frame = CGRectMake(0, 0, 200, 20);
closeButton.center = self.center;
[closeButton setTitle:#"Close" forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(hide) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:closeButton];
}
return self;
}
- (void) show
{
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
{
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
[window addSubview:self];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.2*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounceInStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
[UIView commitAnimations];
}
- (void)bounceInStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(bounceOutStopped)];
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
[UIView commitAnimations];
}
- (void)bounceOutStopped
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.15*BOUNCE_SPEED];
self.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
}
- (void) hide
{
[self removeFromSuperview];
}
#end
Usage
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* popButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
popButton.frame = CGRectMake(0, 0, 200, 20);
popButton.center = self.view.center;
[popButton setTitle:#"Pop" forState:UIControlStateNormal];
[popButton addTarget:self action:#selector(showIt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:popButton];
}
- (void) showIt
{
SimpleDialog* simple = [[SimpleDialog alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
simple.center = self.view.center;
[self.view addSubview:simple];
[simple show];
[simple release];
}
Yes this is possible.
But it requires to customize the UIAlertView.
Using UIAlertView customization you can add anything.

transition animation on only a part of the screen

I am displaying two images in my iPhone app, one on top, one on the bottom. They cover the entire screen. The user, with a swipe gesture, can change either of the images, depending on where the swipe started.
I want the image to change with an animated transition. It currently works without animation, or with the entire screen transitioning. Is it possible to make the transition occur over part of the screen?
I load the images for the first time (in viewDidLoad) thus:
// top image
UIImage *topImage = [UIImage imageNamed:#"top1.png"];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
// bottom image
UIImage *bottomImage = [UIImage imageNamed:#"bottom1.png"];
CGRect bottomframe = CGRectMake(0.0f, 240.0f, 320.0f, 240.0f);
UIImageView *bottomView = [[UIImageView alloc] initWithFrame:bottomframe];
bottomView.image = bottomImage;
[self.view addSubview:bottomView];
[bottomView release];
When I detect a swipe, and detect which of the two images are to be changed, I call a routine like this one:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
CGRect topframe = CGRectMake(0.0f, 0.0f, 320.0f, 240.0f);
UIImageView *topView = [[UIImageView alloc] initWithFrame:topframe];
topView.image = topImage;
[self.view addSubview:topView];
[topView release];
}
Essentilly, I'm continually loading images on top of each other. Is that the best way to do it, especially in terms of memory management?
Everything else I've tried, using techniques such as below, make the entire screen transition:
[UIView beginAnimations:nil context:nil];
...
[UIView commitAnimations];
Thanks for any leads on which way I should be going on this.
well, an easy way could be this (repeat for the bottomImage):
don't release topView when you load it the first time in your method,
but declare it in your .h file,
and use a tempView too:
#interface YourClass: UIViewController{
UIImageView *topView;
UIImageView *tempView;
}
this way you can call them to move them and remove them when you load a new image
then in .m:
EDIT: some correction (see coco's comments):
[a] [b] line 4 = [c] line 11:
- (void)changeTopImage:(NSString *)newImage {
UIImage *topImage = [UIImage imageNamed:newImage];
//CGRect topframe = CGRectMake(0.0f, 0.0f, (320.0f + 320), 240.0f);
CGRect topframe = CGRectMake((0.0f + 320), 0.0f, 320.0f, 240.0f);
tempView = [[UIImageView alloc] initWithFrame:topframe];
//topView.image = topImage;
tempView.image = topImage;
[self.view addSubview:tempView];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[UIView beginAnimations:#"animation" context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
tempView.center = topView.center;
// do this to push old topView away, comment next line if wanna new image just cover it
//topView.center = CGPointMake(topView.x - 320, topView.y);
topView.center = CGPointMake(topView.center.x - 320, topView.center.y);
// call a method when animation has finished:
[UIView setAnimationDidStopSelector:#selector(endOfAnimation:finished:context:)];
[UIView commitAnimations];
}
- (void)endOfAnimation:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
[topView removeFromSuperview];
topView = tempView;
[tempView release];
tempView = nil;
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
- (void)dealloc {
if (tempView != nil) {
[tempView release];
}
[topView release];
[super dealloc];
}