HI everyone I'm french so scuse me for my english SO here is my question: How can we define the height and the width of view. Here it name is flakeView. Here is the code :
UIImageView* flakeView = [[[UIImageView alloc] initWithImage:flakeImage] autorelease];
// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);
// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;
// put the flake in our main view
[self.view addSubview:flakeView];
[UIView animateWithDuration:7
animations:^{
// set the postion where flake will move to
flakeView.center = viewToRotate.center;
}];
CGRect frame = CGRectMake(xPosition, yPosition, width, height);
flakeView.frame = frame;
Note that xPosition and yPosition are the top left corner, not the center.
Related
I have implemented Circular transitions as i asked in
animating UIView transitions like expand dot to circle
using following code:
-(IBAction)nextViewOpen
{
NextViewVC *NextView= [self.storyboard instantiateViewControllerWithIdentifier:#"RBANextViewVC"];
[NextView.view.layer setCornerRadius:25.0f];
[NextView.view.layer setMasksToBounds:YES];
NextView.view.frame=CGRectMake(100,120, 1, 1);
[self.view addSubview:NextView.view];
[self setRoundedView:NextView.view toDiameter:5];
[UIView animateWithDuration:5.0 animations:^{
====== [self setRoundedView:NextView.view toDiameter:480]; ======
}
completion:^(BOOL finished)
{
NextView.view.frame=CGRectMake(0, 0, 320, 480);
NextView.view.layer.cornerRadius = 0;
}
];
}
-(void)setRoundedView:(UIView *)roundedView toDiameter:(float)newSize;
{
CGPoint saveCenter =roundedView.center;
CGRect newFrame = CGRectMake(roundedView.frame.origin.x, roundedView.frame.origin.y, newSize, newSize);
roundedView.frame = newFrame;
//roundedView.layer.cornerRadius = newSize / 2.0;
roundedView.layer.cornerRadius = roundedView.frame.size.width / 2.0;
roundedView.center = saveCenter;
roundedView.clipsToBounds = YES;
}
But it is having some issues
the view gets complete circle shape at the end of the animated transition, initially its not having rounded corners perfectly
I am trying to make an image scroll horizontally to the right while the device is in portrait mode. The image needs to scroll automatically until a timer ends, and needs to scroll seamlessly. How can I achieve this?
Try this,hope this will help.
// Create Image's array
NSMutableArray * imagesArray = [[NSMutableArray alloc]init];
for (int imageCount = 0; imageCount < YOURCOUNT;imageCount++)
{
[imagesArray addObject:[UIImage imageNamed:#"localImagePath%d.png",imageCount]];
}
// Create ScrollView
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0,0.0,320.0,480.0)];
scrollview.contentSize = CGSizeMake(scrollview.frame.size.width * YOURCOUNT,scrollview.frame.size.height);
// Add those image's to the scrollview
CGFloat xPos = 0.0;
for (UIImage * image in imagesArray) {
#autoreleasepool {
UIImageView * imageview = [[UIImageView alloc]initWithImage:image];
imageview.frame = CGRectMake(xPos, 0.0,scrollview.frame.size.width,scrollview.frame.size.height);
[scrollview addSubview:imageview];
xPos += scrollview.frame.size.width;
}
}
[self.view addSubview:scrollview];
// Animate to the Right
[UIView animateWithDuration:10.0 delay:0 options:UIViewAnimationTransitionNone
animations:^{
[scrollview setContentOffset:CGPointMake(scrollview.contentSize.width - scrollview.frame.size.width,0.0) animated:NO];
}
completion:^(BOOL finished) {
NSLog(#"Finished");
}];
I have a UIView that I want to slide from behind a UITabBar to be position right on top of it.
This doesn't work. My view does not appear.
- (void)showNotificationBar
{
CGRect frame = CGRectMake(0, 500, 320, 32);
frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame) - frame.size.height;
notificationBar.frame = frame;
[self.parentViewController.tabBarController.tabBar.superview insertSubview:notificationBar
belowSubview:self.parentViewController.tabBarController.tabBar];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = notificationBar.frame;
frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame);
notificationBar.frame = frame;
}];
}
Initialize frame.origin.y like this:
frame.origin.y = self.tabBarController.tabBar.frame.origin.y;
In the animations block, set it like this:
frame.origin.y -= frame.size.height;
If you want it to show in every view, you could do to things: either show it at the bottom of every view or show it in the application's window. I personally like this second approach better because it helps avoiding duplicate code:
CGFloat notificationBarHeight = 40.0f;
UIView *notificationBar = [[UILabel alloc]initWithFrame:CGRectMake(0, self.tabBarController.tabBar.frame.origin.y - notificationBarHeight, 320, notificationBarHeight)];
[self.window insertSubview:notificationBar atIndex:[[self.window subviews]count]];
[self.view insertSubview:notificationView atIndex:1]; worked for me...
I have a problem with setting new frame to uiview object.
here is my code:
UIImageView *blockImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, [y doubleValue], self.view.frame.size.width, 86)];
[blockImage setImage:[UIImage imageNamed:#"mainscreen_block_sample.jpg"]];
[blockImage setBackgroundColor:[UIColor blackColor]];
//setting anchor point
[blockImage.layer setAnchorPoint:CGPointMake(0.5, 1.0)];
//preparing transform
float distance = 500;
CATransform3D basicTrans = CATransform3DIdentity;
basicTrans.m34 = 1.0 / -distance;
//calculating angle
double OF=oldSize/2.0-difference;
double OC=oldSize/2.0;
double angle= acosf(OF/OC);
angle=angle/2.0;
//transforming
blockImage.layer.transform = CATransform3DRotate(basicTrans, angle, 1.0f, 0.0f, 0.0f);
double newOriginY=[y doubleValue]-difference;
double newFrameHeight=blockImage.frame.size.height;
[blockImage setFrame:CGRectMake(0, newOriginY, blockImage.frame.size.width, newFrameHeight)];
NSLog(#" blockImage frame y: %g", blockImage.frame.origin.y);
NSLog(#" blockImage frame height: %g", blockImage.frame.size.height);
//adding subview
[blocks addObject:blockImage];
[self.view addSubview:[blocks objectAtIndex:([blocks count]-1)]];
//releasing memory
[blockImage release];
The NSLog output is not desirable :(
values of new rect:
newOriginY: 78
newFrameHeight: 77.82
but NSLog is different:
blockImage frame y: 85.0665
blockImage frame height: 70.7535
If you're applying a transform then the frame property should be ignored. You should set the bounds and the center appropriately. The documentation for UIView states this.
I am using an app, in that am image is animating from top to bottom of screen using the animation.
code:
- (void)onTimer
{
// build a view from our flake image
flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.backgroundColor=[UIColor blueColor];
// use the random() function to randomize up our flake attributes
int startX =round(random() % 460);
printf("\n ===== startX :%d",startX);
int endX = round(random() % 460);
printf("\n ===== endX :%d",endX);
double scale = 1 / round(random() % 100) - 1.0;
double speed = 1 / round(random() %100) + 1.0;
// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
flakeView.alpha = 1.0;
// put the flake in our main view
[mView addSubview:flakeView];
[UIView beginAnimations:nil context:flakeView];
[UIView setAnimationDuration:20 * speed];
// code to get current position of image on view
CALayer mainLayer = flakeView.layer.presentationLayer;
CGRect layerFrame = mainLayer.frame;
BOOL intersects = CGRectIntersectsRect(layerFrame, dragger.frame);
// end position on screen
flakeView.frame = CGRectMake(endX, 500.0, 15.0 * scale, 15.0 * scale);
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:#selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
- (id)init
{
if(self = [super init])
{
mView = [[UIView alloc]initWithFrame:CGRectMake(0,0,320,440)];
mView.backgroundColor = [UIColor whiteColor];
mainLayer =[CALayer layer];
// load our flake image we will use the same image over and over
flakeImage = [UIImage imageNamed:#"apple.png"];
[mView.layer addSublayer:mainLayer];
// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(0.8) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
self.view=mView;
}
return self;
}
I used this to get position of an image that is animating from top to bottom of the screen.
but I am getting the connstant values of x and y for the image that is animating.
Can any one help in this.
Thank U.
One thing I have noted from above code is that you are adding flakeView each time on mView. Do you want so? Please try with following code.
- (void)onTimer
{
int startX =round(random() % 460);
printf("\n ===== startX :%d",startX);
int endX = round(random() % 460);
printf("\n ===== endX :%d",endX);
double scale = 1 / round(random() % 100) - 1.0;
double speed = 1 / round(random() %100) + 1.0;
if(flakeView == nil)
{
// build a view from our flake image
flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.backgroundColor=[UIColor blueColor];
// use the random() function to randomize up our flake attributes
// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
flakeView.alpha = 1.0;
// put the flake in our main view
[mView addSubview:flakeView];
}
[UIView beginAnimations:nil context:flakeView];
[UIView setAnimationDuration:20 * speed];
// code to get current position of image on view
CALayer mainLayer = flakeView.layer.presentationLayer;
CGRect layerFrame = mainLayer.frame;
BOOL intersects = CGRectIntersectsRect(layerFrame, dragger.frame);
// end position on screen
flakeView.frame = CGRectMake(endX, 500.0, 15.0 * scale, 15.0 * scale);
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:#selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
NSLog(#"Co-ordinates of imageview : (%f, %f, %f, %f) %#",flakeView.frame.origin.x,flakeView.frame.origin.y,flakeView.frame.size.width,flakeView.frame.size.height);
}
Thanks,
I'm wondering why you could use the dot-notation to access the presentationLayer. It is a method, not a property of CALayer.
Note: "Use of [UIView beginAnimations:context:] is discouraged in iPhone OS 4.0 and later. You should use the block-based animation methods instead." (Apple Docs). If you don't know what block-based animations are, read: What are block-based animation methods in iPhone OS 4.0?
If it still doesn't work with block-based animations, try to set the layer's delegate to nil or use CABasicAnimation either implicitly or explicitly.