I have some strange behavior using a UIPageControl:
First it appears showing only one bullet, then when I move the scroll view all the bullets appear correctly. Is there something I'm missing before I add it as a subview?
Here is my code imageScrollView.h :
#interface ImageScrollView : UIView <UIScrollViewDelegate> {
NSMutableDictionary *photos;
BOOL *pageControlIsChangingPage;
UIPageControl *pageControl;
}
#property (nonatomic, copy) NSMutableDictionary *photos;
#property (nonatomic, copy) UIPageControl *pageControl;
#end
Here is the code for imageScrollView.m:
#import "ImageScrollView.h"
#implementation ImageScrollView
#synthesize photos, pageControl;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code
}
return self;
}
- (void) drawRect:(CGRect)rect
{
[self removeAllSubviews];
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.frame.size.width,self.frame.size.height)];
[scroller setDelegate:self];
[scroller setBackgroundColor:[UIColor grayColor]];
[scroller setShowsHorizontalScrollIndicator:NO];
[scroller setPagingEnabled:YES];
NSUInteger nimages = 0;
CGFloat cx= 0;
for (NSDictionary *myDictionaryObject in photos)
{
if (![myDictionaryObject isKindOfClass:[NSNull class]]) {
NSString *photo =[NSString stringWithFormat:#"http://www.techbase.com.mx/blog/%#",[myDictionaryObject objectForKey:#"filepath"]];
NSDictionary *data = [myDictionaryObject objectForKey:#"data"];
UIView *imageContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 30)];
TTImageView *imageView = [[TTImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 30)];
imageView.urlPath=photo;
[imageContainer addSubview:imageView];
UILabel *caption = [[UILabel alloc] initWithFrame:CGRectMake(0,imageView.frame.size.height,imageView.frame.size.width,10)];
[caption setText:[NSString stringWithFormat:#"%#",[data objectForKey:#"description"]]];
[caption setBackgroundColor:[UIColor grayColor]];
[caption setTextColor:[UIColor whiteColor]];
[caption setLineBreakMode:UILineBreakModeWordWrap];
[caption setNumberOfLines:0];
[caption sizeToFit];
[caption setFont:[UIFont fontWithName:#"Georgia" size:10.0]];
[imageContainer addSubview:caption];
CGRect rect = imageContainer.frame;
rect.size.height = imageContainer.size.height;
rect.size.width = imageContainer.size.width;
rect.origin.x = ((scroller.frame.size.width - scroller.size.width) / 2) + cx;
rect.origin.y = ((scroller.frame.size.height - scroller.size.height) / 2);
imageContainer.frame=rect;
[scroller addSubview:imageContainer];
[imageView release];
[imageContainer release];
[caption release];
nimages++;
cx +=scroller.frame.size.width;
}
}
[scroller setContentSize:CGSizeMake(nimages * self.frame.size.width, self.frame.size.height)];
[self addSubview:scroller];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height -20, (self.frame.size.width/nimages)/2, 20)];
pageControl.numberOfPages=nimages;
[self addSubview:pageControl];
[scroller release];
}
-(void)dealloc {
[pageControl release];
[super dealloc];
}
-(void)scrollViewDidScroll:(UIScrollView *)_scrollView{
if(pageControlIsChangingPage){
return;
}
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth /2) / pageWidth) + 1;
pageControl.currentPage = page;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView{
pageControlIsChangingPage = NO;
}
#end
Since you're drawing the UIPageControl in the drawRect method, you need to call the setNeedsLayout method of this control after you initialize it. Otherwise it won't render itself properly until an event that forces this redrawing is called.
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width/2, self.frame.size.height -20, (self.frame.size.width/nimages)/2, 20)];
pageControl.numberOfPages=nimages;
[pageControl setNeedsLayout];
[self addSubview:pageControl];
Related
I'd like to be able to create elements (like UIViews) when for example the user touches a button
NSMutableString *myVar = [NSMutableString stringWithFormat:#"_view%i", num];
UIView * newView = [self valueForKey:myVar];
but without adding all the
UIView * _view1;
UIView * _view2;
...
in the .h file (if only this is possible..)
You can use an NSMutableArray to hold them. Each time you create a new view just add it to the array.
Here's sample code that should do what you want.
#interface MyViewController ()
#property (strong, nonatomic) NSMutableArray *listChildViews;
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.listChildViews = [[NSMutableArray alloc] init];
}
- (IBAction)addChildViewTapped:(id)sender
{
int numChildViews = [self.listChildViews count];
++numChildViews;
// add new child view
NSString *labelForNewView = [NSString stringWithFormat:#"view %d", numChildViews];
CGFloat labelHeight = 28.0;
UILabel *childView = [[UILabel alloc] initWithFrame:CGRectMake(10, numChildViews*labelHeight, 120.0, labelHeight)];
childView.text = labelForNewView;
[self.listChildViews addObject:childView];
[self.view addSubview:childView];
}
#end
Here is code implementation of pauls answer:
- (IBAction)userTouchedButton:(id)sender{
for (int i = 0; i < 100; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor redColor]];//to distinguish theseViews from other views
[view setTag:i];//to identified it later
[_array insertObject:view atIndex:i];// globleMutble array
[self.view addSubview:view];
}
}
You need not add your views in the .h file. Just instantiate before and where you add them
-(void) addButton
{
UIView *view = [self view];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:#"My Button" forState:UIControlStateNormal];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 0, 0)];
[myLabel setText:#"My Label"];
[button1 sizeToFit];
[myLabel sizeToFit];
[view addSubview:button1];
[view addSubview:myLabel];
}
I am newbie trying to make an app similar to Notes app of iPhone using UITextView.
I am getting the textView and lines and it is working fine.
My problem is that I want to add a UINavigationBar and back button on it.
And I want to add a UIToolBar at the bottom and 2 toolBarItems on it how to do this programmetically. Any help will be a great push up for me..
below is the code snippet.
NoteView.h
#interface NoteView : UITextView <UITextViewDelegate,UITabBarControllerDelegate>
{
}
NoteView.m
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithRed:0.6f green:0.6f blue:1.0f alpha:1.0f];
self.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:20];
self.contentMode = UIViewContentModeRedraw;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
CGContextSetLineWidth(context, 1.0f);
CGContextBeginPath(context);
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;
CGFloat baselineOffset = 6.0f;
for (int x = 0; x < numberOfLines; x++) {
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
}
CGContextClosePath(context);
CGContextStrokePath(context);
}
AddNotesViewController.h
#interface AddNotesViewController : UIViewController <UITextViewDelegate,UITabBarDelegate>
{
NoteView *note;
}
#property (nonatomic, retain) NoteView *note;
#end
AddNotesViewController.m
- (void)loadView
{
[super loadView];
self.note = [[[NoteView alloc] initWithFrame:self.view.bounds] autorelease];
[self.view addSubview:note];
note.delegate = self;
note.text=#"";
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[note setNeedsDisplay];
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
CGRect frame = self.view.bounds;
frame.size.height -= KEYBOARD_HEIGHT;
note.frame = frame;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
note.frame = self.view.bounds;
}
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
Please tell me how and where to add navigation bar , back button and tool bar ,2 toolBarItems on it.Thanks in advance...
Navigation Bar Image
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *image = [UIImage imageNamed:#"TopBar.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Back Button
-(void)getBackBtn
{
UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom];
[Btn setFrame:CGRectMake(0.0f,0.0f,50.0f,30.0f)];
[Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"back.png"]] forState:UIControlStateNormal];
//[Btn setTitle:#"OK" forState:UIControlStateNormal];
//Btn.titleLabel.font = [UIFont fontWithName:#"Georgia" size:14];
[Btn addTarget:self action:#selector(backBtnPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn];
[self.navigationItem setLeftBarButtonItem:addButton];
}
BackButtonAction
-(IBAction)backBtnPress:(id)sender
{
}
View on NavigationBar
For View on navigationBar you can follow my answer Link
use this code....
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(#"Back", #"")
style:UIBarButtonItemStyleDone
target:self
action:#selector(YourActionMethod:)];
self.navigationItem.leftBarButtonItem = addButton;
UITapGestureRecognizer and UIButton are not working together.
UIButton alone is working fine without UITapGesturerecognizer. It shows in all scrolling image views but after adding UITapGestureReconizer feature it is not showing UIButton when tapped.
BOOL numberofTaps;
#interface ImageScrollViewController : UIViewController <UIGestureRecognizerDelegate>
#property (nonatomic, assign) UITapGestureRecognizer *recognizer;
- (void)handleTap:(UIGestureRecognizer*)sender;
//////////////////////////
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
myButton.hidden = YES;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:recognizer];
imageView.userInteractionEnabled = YES;
recognizer.delegate = self;
numberofTaps = 1;
[recognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
// [imageScrollView addGestureRecognizer:tap];
// [imageView addSubview:tap];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
EDIT: Howcome this works but when i only uncomment myButton.hidden= NO then it works but still doesn't shows my button DONE on the imageviews
- (void)handleTap:(UIGestureRecognizer*)sender {
// if(numberofTaps == 1){
CGPoint tapPoint = [sender locationInView:_imageScrollView];
int tapX = (int) tapPoint.x;
int tapY = (int) tapPoint.y;
NSLog(#"TAPPED X:%d Y:%d", tapX, tapY);
//_myButton.hidden = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:nil cancelButtonTitle:#"I'm awesome." otherButtonTitles:nil];
[alert show];
[alert release];
}
What is the reason that otherwise it is working but not showing Done UIButton.
First you have to find button which you have to show. So for that you will have to setTag: on your UIButton before adding them to scrollView
like this -
myButton.tag = i;
Then add tag to your UIImageView also before adding them to scrollView like this -
imageView.tag = i*100;
Now in handleTap: method you can compare tags and can get the button which you have to show.
-(void)handleTap:(UIGestureRecognizer *)sender
{
//getting all buttons of scrollView
for(UIButton *button in scrollView.subviews)
{
//comparing tags
if(button.tag == sender.view.tag/100)
{
button.hidden = NO;
}
}
}
Do this, remove your if Condition.
I want to add infinite scrolling images and there is no end for images scrolling:
const CGFloat kScrollObjHeight = 200.0;
const CGFloat kScrollObjWidth = 320.0;
const NSUInteger kNumImages = 17;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollview1 subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
[scrollview1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollview1 bounds].size.height)];
}
-(void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[scrollview1 setBackgroundColor:[UIColor blackColor]];
[scrollview1 setCanCancelContentTouches:NO];
scrollview1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollview1.clipsToBounds = YES;
scrollview1.scrollEnabled = YES;
scrollview1.pagingEnabled = YES;
for(int i=0;i< kNumImages;i++)
{
NSString *imgName=[[NSString alloc] initWithFormat:#"head%d.png",i];
UIImage *img=[UIImage imageNamed:imgName];
UIImageView *imageView=[[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(i* kScrollObjWidth, 0, kScrollObjWidth,kScrollObjHeight)];
[scrollview1 addSubview:imageView];
[imageView release];
[imgName release];
}
scrollview1.contentSize= CGSizeMake(kScrollObjWidth* kNumImages, kScrollObjHeight);
[self layoutScrollImages];
[super viewDidLoad];
}
Just make a simple new project with .h file as
#import <UIKit/UIKit.h>
#interface scrollableImageViewController : UIViewController {
IBOutlet UIScrollView *sview;
}
#end
and the .m file with this method
- (void)viewDidLoad {
[super viewDidLoad];
for(int i=0;i<10;i++)
{
NSString *name=[[NSString alloc] initWithFormat:#"images%d.png",i];
NSLog(#"%#",name);
[name release];
UIImage *img=[UIImage imageNamed:#"img.png"];
UIImageView *imageView=[[UIImageView alloc] initWithImage:img];
[imageView setFrame:CGRectMake(i*57, 0, 57, 57)];
[sview addSubview:imageView];
[imageView release];
}
sview.contentSize= CGSizeMake(57*10, 57);
}
and check the result
You can check out Apples sample code PhotoScroller.
It's the code they introduced at WWDC 11
in Session 104 - Advanced ScrollView Techniques. You can watch this video (and the corresponding PDF) for free if you are a registered Apple developer.
I am using UIScrollView.In that scrollview I placing UIImageViews in serial order.Zooming is not working .This is my code
- (void)viewDidLoad
{
self.view.backgroundColor=[UIColor blackColor];
NSArray *imageArray=[[NSArray alloc]initWithObjects:[UIImage imageNamed:#"test1.png"],[UIImage imageNamed:#"test2.png"],[UIImage imageNamed:#"test3.png"], nil];
scrollView_=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width+20, self.view.frame.size.height)];
[scrollView_ setCanCancelContentTouches:NO];
scrollView_.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView_.clipsToBounds = NO;
scrollView_.scrollEnabled = YES;
scrollView_.pagingEnabled = YES;
scrollView_.delegate=self;
scrollView_.showsHorizontalScrollIndicator=NO;
scrollView_.showsVerticalScrollIndicator=NO;
scrollView_.contentSize = CGSizeMake([imageArray count]*self.scrollView_.frame.size.width, self.scrollView_.frame.size.height);
[scrollView_ setMaximumZoomScale:3.0];
[scrollView_ setMinimumZoomScale:1];
[scrollView_ setZoomScale:1];
[scrollView_ setBouncesZoom:YES];
[scrollView_ setBounces:YES];
for(int i=0;i<[imageArray count];i++)
{
UIImageView *imageView=[[UIImageView alloc]initWithImage:[imageArray objectAtIndex:i]];
imageView.frame=CGRectMake((i*320)+(i*20), 0, 320, 480);
[imageView_ setTag:i];
[scrollView_ addSubview:imageView];
[imageView release];
}
[self.view addSubview:scrollView_];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return [scrollView_ viewWithTag:page_];
}
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.page_=page;
}
Thanks in advance..
Where have you set -
[self.mainScroll setZoomScale:(float)];
I guess by default this is 1. So no zoom happens...
Create a container view, and add imageViews into the container view,
then use the container view as zoomview.
- viewDidLoad {
// ---
self.containerView =[[[UIView alloc] init] autorelease];
self.containerView.frame = CGRectMake(0, 0, [imageArray count]*self.scrollView_.frame.size.width, self.scrollView_.frame.size.height);
[scrollView_ addSubView:self.containerView];
for(int i=0;i<[imageArray count];i++)
{
UIImageView *imageView=[[UIImageView alloc]initWithImage:[imageArray objectAtIndex:i]];
imageView.frame=CGRectMake((i*320)+(i*20), 0, 320, 480);
[imageView_ setTag:i];
[self.containerView addSubview:imageView];
[imageView release];
}
// --
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.containerView;
}