I want to place imageview at different position with animation - iphone

I am trying to set multiple image on different position this is my code anyone help me to set imageview on different position.
- (void)viewDidLoad
{
[super viewDidLoad];
frameX = 45.0f;
frameY = 60.0f;
imagearr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"King2.png"],
[UIImage imageNamed:#"3.png"],[UIImage imageNamed:#"queen.png"],
[UIImage imageNamed:#"King.png"],nil];
}
-(IBAction)hidebtn:(id)sender
{
[btn setHidden:TRUE];
[self setCards];
}
-(void)setCards
{
CGFloat dx = self.view.frame.size.width/2;
CGFloat dy = self.view.frame.size.height/2;
[cards setAnimationImages:imagearr];
CGRect frame = cards.frame;
frame.size.width = frameX;
frame.size.height = frameY;
[cards setFrame:frame];
cards.animationRepeatCount = 1;
for (int i=0; i<5;i++)
{
[UIView beginAnimations:#"Move" context:nil];
[UIView setAnimationDuration:0.3];
switch (i)
{
- (void)viewDidLoad
{
[super viewDidLoad];
frameX = 45.0f;
frameY = 60.0f;
imagearr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"King2.png"],
[UIImage imageNamed:#"3.png"],[UIImage imageNamed:#"queen.png"],
[UIImage imageNamed:#"King.png"],nil];
}
-(IBAction)hidebtn:(id)sender
{
[btn setHidden:TRUE];
[self setCards];
}
-(void)setCards
{
CGFloat dx = self.view.frame.size.width/2;
CGFloat dy = self.view.frame.size.height/2;
[cards setAnimationImages:imagearr];
CGRect frame = cards.frame;
frame.size.width = frameX;
frame.size.height = frameY;
[cards setFrame:frame];
cards.animationRepeatCount = 1;
for (int i=0; i<5;i++)
{
[UIView beginAnimations:#"Move" context:nil];
[UIView setAnimationDuration:0.3];
switch (i)
{
case 0:
[cards setCenter:CGPointMake(dx, dy-120.0)];
cards.transform = CGAffineTransformMakeRotation(0);
break;
default:
break;
}
[self.view addSubview:cards];
[UIView commitAnimations];
}
default:
break;
}
[self.view addSubview:cards];
[UIView commitAnimations];
}

You are adding subviews too late, you can't animate what is not on screen. Add them before you start animations on some position then animate to the target position.

Related

Set Admob banner to appear at bottom of screen

I am currently using this code to display an Admob request;
If I use this code, the banner appears, but at the top of the screen not the bottom;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGFloat screenXPos = (screenWidth/2);
CGFloat screenYPos = screenHeight - kGADAdSizeBanner.size.height;
[bannerView_ setCenter:CGPointMake(screenXPos, screenYPos)];
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
bannerView_.adUnitID = ADMOB_ID;
bannerView_.rootViewController = self;
bannerView_.adUnitID = ADMOB_ID;
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
How would I change this to show at the bottom of the screen instead?
METHOD 1: Put this code in bottom of your code.
//TRY_1:
bannerView_.frame = CGRectMake(screenWidth/2.0 - bannerView_.frame.size.width/2.0, screenHeight - bannerView_.frame.size.height,
bannerView_.frame.size.width,bannerView_.frame.size.height);
//OR TRY_2:
bannerView_.frame = CGRectMake(screenHeight/2.0 - bannerView_.frame.size.width/2.0, screenWidth - bannerView_.frame.size.height,
bannerView_.frame.size.width,bannerView_.frame.size.height);
METHOD 2: Detailed clean way
typedef enum _bannerType
{
kBanner_Portrait_Top,
kBanner_Portrait_Bottom,
kBanner_Landscape_Top,
kBanner_Landscape_Bottom,
}CocosBannerType;
//In .h declare CocosBannerType mBannerType;
-(void)createAdmobAds
{
mBannerType = kBanner_Portrait_Bottom; //kBanner_Portrait_Top
AppController *app = (AppController*)[[UIApplication sharedApplication] delegate];
// Create a view of the standard size at the bottom of the screen.
// Available AdSize constants are explained in GADAdSize.h.
if(mBannerType <= kBanner_Portrait_Bottom)
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
else
mBannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
mBannerView.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
mBannerView.rootViewController = app.navController;
[app.navController.view addSubview:mBannerView];
// Initiate a generic request to load it with an ad.
[mBannerView loadRequest:[GADRequest request]];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGSize s = screenRect.size;
CGRect frame = mBannerView.frame;
off_x = 0.0f;
on_x = 0.0f;
switch (mBannerType)
{
case kBanner_Portrait_Top:
{
off_y = -frame.size.height;
on_y = 0.0f;
}
break;
case kBanner_Portrait_Bottom:
{
off_y = s.height;
on_y = s.height-frame.size.height;
}
break;
case kBanner_Landscape_Top:
{
off_y = -frame.size.height;
on_y = 0.0f;
}
break;
case kBanner_Landscape_Bottom:
{
off_y = s.height;
on_y = s.height-frame.size.height;
}
break;
default:
break;
}
frame.origin.y = off_y;
frame.origin.x = off_x;
mBannerView.frame = frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
frame = mBannerView.frame;
frame.origin.x = on_x;
frame.origin.y = on_y;
mBannerView.frame = frame;
[UIView commitAnimations];
}
-(void)showBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = on_y;
frame.origin.x = on_x;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)hideBannerView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = off_y;
frame.origin.x = off_x;
}
completion:^(BOOL finished)
{
}];
}
}
-(void)dismissAdView
{
if (mBannerView)
{
[UIView animateWithDuration:0.5
delay:0.1
options: UIViewAnimationCurveEaseOut
animations:^
{
CGRect frame = mBannerView.frame;
frame.origin.y = off_y;
frame.origin.x = off_x;
mBannerView.frame = frame;
}
completion:^(BOOL finished)
{
[mBannerView setDelegate:nil];
[mBannerView removeFromSuperview];
mBannerView = nil;
}];
}
}
You can check the solution proposed in Google Ads developer blog:
http://googleadsdeveloper.blogspot.com/2012/06/keeping-smart-banner-docked-to-bottom.html
// Initialize the banner docked to the bottom of the screen.
// We start in a portrait orientation so use kGADAdSizeSmartBannerPortrait.
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(
kGADAdSizeSmartBannerPortrait).height);
self.adBanner = [[[GADBannerView alloc]
initWithAdSize:kGADAdSizeSmartBannerPortrait
origin:origin] autorelease];
//Continue rest of initialization here
Make sure to use self.view.frame.size.width in the Y coordinate of the origin if you are using kGADAdSizeSmartBannerLandscape.

Customize Scrolling indicator

Is there a way to customize scrolling indicator(vertical or horizontal) in UIScrollView. For example, change it's color or position in scrollview.
First, you have to uncheck showVerticalIndicator and showHorizontallIndicator in the Interface Builder.
Then in the viewDidLoad method add :
[super viewDidLoad];
self.scrollViewBarImgView = [[UIImageView alloc] init];
UIImage *imgBar = [UIImage imageNamed:#"scroller.png"];
[self.scrollViewBarImgView setImage:imgBar];
CGRect frame = scrollViewBarImgView.frame;
frame.size.width = 8;
frame.size.height = 60;
frame.origin.x = 312;
frame.origin.y = 0;
[self.scrollViewBarImgView setFrame:frame];
[self.tableView addSubview:scrollViewBarImgView];
then in the - (void)scrollViewDidScroll method :
CGPoint offset = scrollView.contentOffset;
CGRect frame = self.scrollViewBarImgView.frame;
float fact = (cellHeight*numberOfRow+ indicatorBarHeight) / tableViewHeight;
frame.origin.y = offset.y + (offset.y/fact);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
[self.scrollViewBarImgView setFrame:frame];
[UIView commitAnimations];
This is working for me.

keyboards hide entering text

I am working on QuickDialog at here
However, my view can not scroll and the keyboard is hiding the entering text. What I am having so far is
- (void)loadView {
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.navigationItem.hidesBackButton = NO;
NSLog(#"LOAD VIEW");
self.root = [[QRootElement alloc] init] ;
self.root.controllerName = #"CreateAccountViewController";
self.root.grouped = YES;
self.root.title = #"Registration";
[super loadView];
QSection *section1 = [[QSection alloc] init];
// Create title cell
QEntryElement *cell1 = [[QEntryElement alloc] init];
cell1.title = #"Title: ";
cell1.key = #"title";
cell1.placeholder = #"Mr/Mrs/Miss";
cell1.hiddenToolbar = YES;
cell1.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell1.autocorrectionType = UITextAutocorrectionTypeNo;
// Create firstName cell
QEntryElement *cell2 = [[QEntryElement alloc] init];
cell2.title = #"FirstName: ";
cell2.key = #"firstName";
cell2.placeholder = #"Enter your first name";
cell2.hiddenToolbar = YES;
cell2.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell2.autocorrectionType = UITextAutocorrectionTypeNo;
// Create lastName cell
QEntryElement *cell3 = [[QEntryElement alloc] init];
cell3.title = #"LastName: ";
cell3.key = #"lastName";
cell3.placeholder = #"Enter your last name";
cell3.hiddenToolbar = YES;
cell3.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell3.autocorrectionType = UITextAutocorrectionTypeNo;
// Create phone number cell
QEntryElement *cell4 = [[QEntryElement alloc] init];
cell4.title = #"Phone: ";
cell4.key = #"phoneNumber";
cell4.placeholder = #"Enter your phone number";
cell4.hiddenToolbar = YES;
cell4.autocapitalizationType = UITextAutocapitalizationTypeNone;
cell4.autocorrectionType = UITextAutocorrectionTypeNo;
.............................
.............................
[main addElement:cell1];
[main addElement:cell2];
[main addElement:cell3];
[main addElement:cell4];
[main addElement:cell5];
[main addElement:cell6];
[main addElement:cell7];
[self.root addSection:section];
After running it, i realized that the keyboard is hiding the context because I have a long list of input. Also, the view is not scrollable at all
There are an attached image at here that you can see my problem...
![enter image description here][3]
I went thru the sample code and the view is scrollable so that the keyboard is not going to hide any text at all.
Does any body have any ideas about this issue, please help. All comments are welcomed here
Just for this record, since this question was asked, this has been implemented in QuickDialog, so you don't have to worry about it anymore.
One way to solve this problem is to resize the scroll view to [screen height] - [height of keyboard] (this is coming from a similar post that I saw somewhere).
You might want to try that.
There are 2 possible ways.
To change the view height when keyboard opens or closes.
To change the view origin when keyboard opens or closes.
Implementing this code in the two delegate methods of your textField will do what you want. Here is the textFieldDidBeginEditing:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect =
[self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect =
[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator =
midline - viewRect.origin.y
- MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator =
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
* viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
And here is the textFieldDidEndEditing:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[self textFieldShouldReturn:textField];
[UIView commitAnimations];
}
If you want it for the buttons you can just setup an IBAction with the above code.

iPhone: TableView inside UIScrollview, show vaccillating scrollbars around

I have added some table and other vies in a scrollview. scrolling in tables are working fine. But in the parent scrollview, when scrolling, a vacillating vertical scrollbars are shown, sometimes it even come to the middle of the screen. sometime show at the left side of the screen. and not limited to the vertical scrollbar region. When I set the showsVerticalScrollIndicator = NO, it is not shown. But do you know why the scrollbar is moving around.
DashboardView is a subclass of UIScrollView.
dashboard=[[DashboardView alloc] initWithFrame:fullScreenRect];
dashboard.contentSize = CGSizeMake(320,700); // must do!
dashboard.showsVerticalScrollIndicator = YES;
dashboard.bounces = YES;
self.view = dashboard;
#implementation DashboardView
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code
}
- (void) layoutSubviews{
NSArray *views = self.subviews;
[UIView beginAnimations:#"CollapseExpand" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
UIView *view = [views objectAtIndex: 0];
CGRect rect = view.frame;
for (int i = 1; i < [views count]; i++ ) {
view = [views objectAtIndex:i];
view.frame = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height, view.frame.size.width, view.frame.size.height);
rect = view.frame;
}
[UIView commitAnimations];
}
Remember that scrollbars is subviews of scroll too. Exclude it from views array, before doing something with views.
Yap. that solved my problem. It seems Scrollbars are a kind of UIImageView. If anybody need it, this is my new code which works fine.
(void) layoutSubviews{
NSArray *views = self.subviews;
[UIView beginAnimations:#"CollapseExpand" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
//CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
// [self.view insertSubview:balanceView belowSubview:profileView];
UIView *view = [views objectAtIndex: 0];
CGRect rect = view.frame;
for (int i = 1; i < [views count]; i++ ) {
view = [views objectAtIndex:i];
if ([view isMemberOfClass: [UIView class] ]) {
//CFShow(view);
view.frame = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height, view.frame.size.width, view.frame.size.height);
rect = view.frame;
}
}
[UIView commitAnimations];
}

Any good way to position UITableViewCellEditingStyleDelete icon within a grouped tableview?

as title, can i position the UITableViewCellEditingStyleDelete button/icon to be within the tableviewcell of a grouped tableview rather than outside of it?
An excerpt from a Matt Gallagher blog post reveals a method
This is the original code to mimic the behavior you DON'T want:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[self setNeedsLayout];
}
- (void)layoutSubviews
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[super layoutSubviews];
if (((UITableView *)self.superview).isEditing)
{
CGRect contentFrame = self.contentView.frame;
contentFrame.origin.x = EDITING_HORIZONTAL_OFFSET;
self.contentView.frame = contentFrame;
}
else
{
CGRect contentFrame = self.contentView.frame;
contentFrame.origin.x = 0;
self.contentView.frame = contentFrame;
}
[UIView commitAnimations];
}
So if we made a change, we can make it do what you do want:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[self setNeedsLayout];
}
- (void)layoutSubviews
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[super layoutSubviews];
if (((UITableView *)self.superview).isEditing)
{
//don't resize and and move your frame here
CGRect contentFrame = self.contentView.frame;
contentFrame.origin.x = 0;
self.contentView.frame = contentFrame;
}
else
{
CGRect contentFrame = self.contentView.frame;
contentFrame.origin.x = 0;
self.contentView.frame = contentFrame;
}
[UIView commitAnimations];
}
You'll proabably have to tweak this, but it is a good start.