page control in uiscrollview - objective-c

i ve created a uiscrollview containing a page control which loads the images from resource bundle..everything works good.i m able to scroll through different images..the problem is if i m to click the corresponding pagecontrol(dot), i m not able to navigate to the corresponding image....could u guys help me out below is the code...the below code works perfectly fine
// Email.h
#interface Email : UIViewController<UIScrollViewDelegate>
{
UIPageControl *pageControl;
UIScrollView *scroller;
}
#property (nonatomic,retain)IBOutlet UIPageControl *pageControl;
#property (nonatomic,retain)IBOutlet UIScrollView *scroller;
-(IBAction)clickPageControl:(id)sender;
#end
// Email.m
#implementation Email
#synthesize pageControl,scroller;
-(IBAction)clickPageControl:(id)sender
{
int page=pageControl.currentPage;
CGRect frame=scroller.frame;
frame.origin.x=frame.size.width=page;
frame.origin.y=0;
[scroller scrollRectToVisible:frame animated:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x/scrollView.frame.size.width;
pageControl.currentPage=page;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title=#"Press Photos";
for (int i=1; i<10; i++)
{
UIImageView *images=[[UIImageView alloc]initWithImage:
[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg",i]]];
images.frame=CGRectMake((i-1)*320, 0, 320, 460);
[scroller addSubview:images];
[images release];
}
scroller.delegate=self;
scroller.contentSize=CGSizeMake(320*9, 460);
scroller.pagingEnabled=YES;
pageControl.numberOfPages=9;
pageControl.currentPage=0;
}

frame.origin.x=frame.size.width=page;
should be
frame.origin.x = frame.size.width * page;

Related

NSCollectionView Reloads content but not the [sender tag]

i am having a programatically created NSCollectionview
_upcomingVideosCollection = [[NSCollectionView alloc] initWithFrame:NSRectFromCGRect(CGRectMake(0, 0, 300, 1200))];
_upcomingVideosCollectionItem = [Videos new];
Videos.m
#import "Videos.h"
#property (strong, retain) IBOutlet NSButton *href_reject;
#end
int i=0;
#implementation Videos
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)setRepresentedObject:(id)representedObject{
[super setRepresentedObject:representedObject];
if (representedObject !=nil)
{
[_href_reject setTag:i];
i=i+1;
}
}
- (IBAction)removeVideo:(id)sender {
nslog(#"%d",[sender tag])
}
#end
When i am trying to load new data in the collectionview using
[_upcomingVideosCollection setItemPrototype:[Videos new]];
[_upcomingVideosCollection setContent:contents];
the new items [sender tag] are not refreshed and they increment
Ex: first data load, i have 3 objects in contents so the tag would be from 0-2
When i am loading another contents ex:10 objects, instead of 0-9 i have 0-11.
**UPDATE:
#vitormm
Solved the problem using [collection setContent:#[]]; before the real setContent

How to add swiple gesture with 3 buttons in a xib?

I have a table view where i m adding a cell for the content of the table view and i need to add swipeable gesture with 3 buttons in that that table cell.
u haven't provide enough info about how you are implementing the tableview cell but i assume that u are using the xib file and also i assume that u want to display the 3 button when table view cell is swiped ..
first of all u should subclass the table view cell in my example i named the cell class as CustomTableCell
in this cell's xib file i am adding the a view and 3 buttons, it is something look like below image
in the above image swipeView should be on top of content view and it holds your swipe gestures
for example in CustomTableCell.h file
#import <UIKit/UIKit.h>
#interface CustomTableCell : UITableViewCell
+ (id)createMenuCell;
#property (weak, nonatomic) IBOutlet UIView *swipeView;
#property (weak, nonatomic) IBOutlet UIButton *button3;
#property (weak, nonatomic) IBOutlet UIButton *button2;
#property (weak, nonatomic) IBOutlet UIButton *button1;
#property (assign, nonatomic) BOOL showButton;
- (IBAction)buttonOneAction:(id)sender;
- (IBAction)buttonTwoAction:(id)sender;
- (IBAction)buttonThreeAction:(id)sender;
#end
and in CustomTableCell.m file
#import "CustomTableCell.h"
#implementation CustomTableCell
#define MAX_LEFT 160 //set the how much view has to be move
#define ANIMATION_DUR 0.3
+ (id)createMenuCell
{
NSArray *xibElements = [[NSBundle mainBundle] loadNibNamed:#"CustomTableCell" owner:nil options:nil];
for(id item in xibElements)
{
if([item isKindOfClass:[CustomTableCell class]])
{
return (CustomTableCell *)item;
}
}
return nil;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
[self setUpGestures];
}
return self;
}
- (void)awakeFromNib {
// Initialization code
_showButton = NO; //simple avoid unwanted swipes when showing the buttons
[self setUpGestures];
}
- (void)setUpGestures{
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeLeftGestureAction)]; //to open
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft; //set the direction to swipe
[self.swipeView addGestureRecognizer:swipeLeftGesture];
UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRightGestureAction)]; //this is for closing
[self.swipeView addGestureRecognizer:swipeRightGesture];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//button actions
- (IBAction)buttonOneAction:(id)sender {
NSLog(#"button one action");
}
- (IBAction)buttonTwoAction:(id)sender {
NSLog(#"button two action");
}
- (IBAction)buttonThreeAction:(id)sender {
NSLog(#"button three action");
}
//hear u are showing the buttons with left swipe
- (void)swipeLeftGestureAction
{
if(!_showButton)
{
CGRect destRect = self.swipeView.frame;
destRect.origin.x = -MAX_LEFT;
[UIView animateWithDuration:ANIMATION_DUR animations:^{
self.swipeView.frame = destRect;
} completion:^(BOOL finished) {
_showButton = YES;
}];
}
}
//hear u are hiding the buttons if it is shown
- (void)swipeRightGestureAction
{
if(_showButton)
{
CGRect destRect = self.swipeView.frame;
destRect.origin.x = 0;
[UIView animateWithDuration:ANIMATION_DUR animations:^{
self.swipeView.frame = destRect;
} completion:^(BOOL finished) {
_showButton = NO;
}];
}
}
Note: above is just i assumed so final result will be like below

iOS: Styling multiple textfields

I have multiple UITextFields with the same/similar styling but I'm styling them individually. How would I style them all in on go?
example of what I'm doing now:
textfield1.layer.borderWith = 2;
textfield2.layer.borderWith = 2;
textfield3.layer.borderWith = 2;
Here's something I've used in the past:
UITextField * textField1;
UITextField * textField2;
UITextField * textField3;
for (UITextField * textField in #[textField1, textField2, textField3]) {
textField.layer.borderWidth = 2;
}
If you're doing extensive edits to create a highly customized textField, it might be better to use a subclass. If you've just got a few additions you want applied to each textfield, this should be fine.
As per your request, because I'm feeling generous. Here's an example of a UITextField subclass.
MyTextField.h
#import <UIKit/UIKit.h>
#interface MyTextField : UITextField
#end
MyTextField.m
#import "MyTextField.h"
#implementation MyTextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.layer.borderWidth = 2;
// Blue For Demonstration
self.layer.borderColor = [UIColor blueColor].CGColor;
// add whatever else you want to customize here ....
}
return self;
}
////////
////// ** Include Custom Methods You Might Need **
////////
#end
Then, in whatever ViewController or View you want to use it, add this:
#import "MyTextField.h"
Then, you can launch MyTextField class Textfields like this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
MyTextField * textField1 = [MyTextField new];
textField1.frame = CGRectMake(10, 30, 100, 30);
[self.view addSubview:textField1];
MyTextField * textField2 = [MyTextField new];
textField2.frame = CGRectMake(10, 70, 100, 30);
[self.view addSubview:textField2];
MyTextField * textField3 = [MyTextField new];
textField3.frame = CGRectMake(10, 110, 100, 30);
[self.view addSubview:textField3];
}
And you'll get this:
How about --
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
textField.layer.borderWidth = 2.0;
}
}
That way you will not even have to have outlets of textFields in the view. But this is, of course, only applicable if you want to apply same styles to every textField in the view.
If the textfields have outlets, create an IBOutletCollection like so in your .h:
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *textFields;
Then, in your .m:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for(UITextField *textField in self.textFields) {
textfield.layer.borderWith = 2;
// add more here as needed
}
}

Infinite UIScrollView not working on iOS 4.3

I try to build a endless scrolling UIScrollView. So far I took the apple sample "StreetScroller". So all I do is setting the contentOffset back when it reaches the end of the scroll view.
Override -layoutSubviews of the UIScrollView:
- (void)layoutSubviews
{
CGFloat contentWidth = [self contentSize].width;
CGPoint contentOffset = [self contentOffset];
CGFloat centerOffsetX = (contentWidth - [self bounds].size.width) / 2.0;
CGFloat distanceFromCenter = contentOffset.x - centerOffsetX;
if (ABS(distanceFromCenter) > (contentWidth / 4.0)) {
contentOffset = CGPointMake(centerOffsetX, contentOffset.y);
[super setContentOffset:contentOffset];
}
}
Now on iOS 5 this works like a charm. But on iOS 4.3 it's not working. As soon as I call [super setContentOffset:contentOffset] it stoops scrolling because next time -layoutSubviews get's called the [self contentOffset] does not return the contentOffset that was set.
I know there are a lot a questions about infinite UIScrollViews, but one of these has fixed this problem!
Try this One. This Code is Working Properly for me on iOS 4.3
RootViewController.h
#class ViewControllerForDuplicateEndCaps;
#interface RootViewController : UIViewController {
ViewControllerForDuplicateEndCaps *viewControllerForDuplicateEndCaps;
}
#property (nonatomic, retain) ViewControllerForDuplicateEndCaps *viewControllerForDuplicateEndCaps;
- (IBAction)loadScrollViewWithDuplicateEndCaps:(id)sender;
#end
RootViewController.m
#import "RootViewController.h"
#import "ViewControllerForDuplicateEndCaps.h"
#import "InfiniteScrollViewAppDelegate.h"
#implementation RootViewController
#synthesize viewControllerForDuplicateEndCaps;
- (IBAction)loadScrollViewWithDuplicateEndCaps:(id)sender {
InfiniteScrollViewAppDelegate *delegate = (InfiniteScrollViewAppDelegate*)[[UIApplication sharedApplication] delegate];
if(self.viewControllerForDuplicateEndCaps == nil) {
ViewControllerForDuplicateEndCaps *temp = [[ViewControllerForDuplicateEndCaps alloc] initWithNibName:#"ViewControllerForDuplicateEndCaps" bundle:nil];
self.viewControllerForDuplicateEndCaps = temp;
[temp release];
}
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[delegate.navigationController pushViewController:self.viewControllerForDuplicateEndCaps animated:YES];
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
#end
ViewControllerForDuplicateEndCaps.h
#import <UIKit/UIKit.h>
#interface ViewControllerForDuplicateEndCaps : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView;
}
#property (nonatomic, retain) UIScrollView *scrollView;
- (void)addImageWithName:(NSString*)imageString atPosition:(int)position;
#end
ViewControllerForDuplicateEndCaps.m
#import "ViewControllerForDuplicateEndCaps.h"
#implementation ViewControllerForDuplicateEndCaps
#synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// add the last image (image4) into the first position
[self addImageWithName:#"image4.jpg" atPosition:0];
// add all of the images to the scroll view
for (int i = 1; i < 5; i++) {
[self addImageWithName:[NSString stringWithFormat:#"image%i.jpg",i] atPosition:i];
}
// add the first image (image1) into the last position
[self addImageWithName:#"image1.jpg" atPosition:5];
scrollView.contentSize = CGSizeMake(1920, 416);
[scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO];
}
- (void)addImageWithName:(NSString*)imageString atPosition:(int)position {
// add image to scroll view
UIImage *image = [UIImage imageNamed:imageString];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(position*320, 0, 320, 416);
[scrollView addSubview:imageView];
[imageView release];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
NSLog(#"%f",scrollView.contentOffset.x);
// The key is repositioning without animation
if (scrollView.contentOffset.x == 0) {
// user is scrolling to the left from image 1 to image 4
// reposition offset to show image 4 that is on the right in the scroll view
[scrollView scrollRectToVisible:CGRectMake(1280,0,320,416) animated:NO];
}
else if (scrollView.contentOffset.x == 1600) {
// user is scrolling to the right from image 4 to image 1
// reposition offset to show image 1 that is on the left in the scroll view
[scrollView scrollRectToVisible:CGRectMake(320,0,320,416) animated:NO];
}
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
#end

How can I display my images in my viewController the same as the below screen shot?

In my iPad program I have an array which holds images. How can I display my all images (from an image array) in my viewController the same as the below screen shot? Is there a good way to do it, any sample application paths to refer or sample code?
I need the following functionality.
Tapping an image will show it full screen
A close button to close this view as the same as the screen shot.
Two buttons to display the remaining and previous images.
A sample screen shot is attached below. We can see that the below application is showing all images at the right side of the screen.
Alright, this should be easy, though a bit of coding is needed.
Start out with a simple view based app template.
I don't know where the images come from so I just put all the images to be shown in the resources folder.
The view controller PictureViewController will accept an array of UIImages and should be presented modally. I'll post the code below.
The code to show the PictureViewController is placed in the view controller created by the template. I just added a simple UIButton to the view to trigger an action which looks something like this:
- (IBAction)onShowPictures
{
// Load all images from the bundle, I added 14 images, named 01.jpg ... 14.jpg
NSMutableArray *images = [NSMutableArray array];
for (int i = 1; i <= 14; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%02d.jpg", i]]];
}
PictureViewController *picViewController = [[PictureViewController alloc] initWithImages:images];
[self presentModalViewController:picViewController animated:YES];
[picViewController release];
}
The view controller's view was created with interface builder, looking like this:
PictureViewController.xib
View Hierarchy
The fullscreen image is linked to an outlet fullScreenImage seen in the following header file. Actions are connected as well.
I've set the fullscreen imageView's content mode to Aspect Fit.
The Thumbnails will be added dynamically with code. Here's the view controller's code
PictureViewController.h
#interface PictureViewController : UIViewController
{
NSMutableArray *imageViews;
NSArray *images;
UIImageView *fullScreenImage;
int currentPage;
}
#property (nonatomic, retain) NSMutableArray *imageViews;
#property (nonatomic, retain) NSArray *images;
#property (nonatomic, retain) IBOutlet UIImageView *fullScreenImage;
- (id)initWithImages:(NSArray *)images;
- (IBAction)onClose;
- (IBAction)onNextThumbnails;
- (IBAction)onPreviousThumbnails;
#end
PictureViewController.m
The define MAX_THUMBNAILS on top defines the maximum of thumbnails seen. A UITapGestureRecognizer will take care of the tap events for the thumbnails. Adjust the CGRectMake in setupThumbnailImageViews to position the thumbnails as you wish. This controller is just a basic approach without orientation support.
#import "PictureViewController.h"
#define MAX_THUMBNAILS 6
#interface PictureViewController ()
- (void)showSelectedImageFullscreen:(UITapGestureRecognizer *)gestureRecognizer;
- (void)setupThumbnailImageViews;
- (void)setThumbnailsForPage:(int)aPage;
- (UIImage *)imageForIndex:(int)anIndex;
#end
#implementation PictureViewController
#synthesize imageViews, images, fullScreenImage;
- (id)initWithImages:(NSArray *)someImages
{
self = [super init];
if (self) {
self.images = someImages;
self.imageViews = [NSMutableArray array];
currentPage = 0;
}
return self;
}
- (void)viewDidLoad
{
self.fullScreenImage.image = [images objectAtIndex:0];
[self setupThumbnailImageViews];
[self setThumbnailsForPage:0];
}
- (void)showSelectedImageFullscreen:(UITapGestureRecognizer *)gestureRecognizer
{
UIImageView *tappedImageView = (UIImageView *)[gestureRecognizer view];
fullScreenImage.image = tappedImageView.image;
}
- (void)setupThumbnailImageViews
{
for (int i = 0; i < MAX_THUMBNAILS; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0,
166.0 + (130.0 * i),
130.0,
90.0)];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(showSelectedImageFullscreen:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
tapGestureRecognizer.numberOfTouchesRequired = 1;
[imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];
imageView.userInteractionEnabled = YES;
[imageViews addObject:imageView];
[self.view addSubview:imageView];
}
}
- (void)setThumbnailsForPage:(int)aPage
{
for (int i = 0; i < MAX_THUMBNAILS; i++) {
UIImageView *imageView = (UIImageView *)[imageViews objectAtIndex:i];
UIImage *image = [self imageForIndex:aPage * MAX_THUMBNAILS + i];
if (image) {
imageView.image = image;
imageView.hidden = NO;
} else {
imageView.hidden = YES;
}
}
}
- (UIImage *)imageForIndex:(int)anIndex
{
if (anIndex < [images count]) {
return [images objectAtIndex:anIndex];
} else {
return nil;
}
}
#pragma mark -
#pragma mark user interface interaction
- (IBAction)onClose
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)onNextThumbnails
{
if (currentPage + 1 <= [images count] / MAX_THUMBNAILS) {
currentPage++;
[self setThumbnailsForPage:currentPage];
}
}
- (IBAction)onPreviousThumbnails
{
if (currentPage - 1 >= 0) {
currentPage--;
[self setThumbnailsForPage:currentPage];
}
}
#pragma mark -
#pragma mark memory management
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[images release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
[imageViews release];
[fullScreenImage release];
}
- (void)dealloc
{
[images release];
[imageViews release];
[fullScreenImage release];
[super dealloc];
}
#end
The result: