I am having problem with detecting swipe on UIImageView which is inside self.view.
When I apply UISwipeGestureRecognizer on self.view it works fine, but when i try to apply same on UIImageview it detects wrong swipe direction.
Code:
{
//Swipe Up
swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeUp:)];
swipeUpRecognizer.delegate = self;
swipeUpRecognizer = (UISwipeGestureRecognizer *)swipeUpRecognizer;
swipeUpRecognizer.numberOfTouchesRequired = 1;
swipeUpRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[imageView addGestureRecognizer:swipeUpRecognizer];
[swipeUpRecognizer release];
//swipe Down
swipeDownRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeDown:)];
swipeDownRecognizer.delegate = self;
swipeDownRecognizer = (UISwipeGestureRecognizer *)swipeDownRecognizer;
swipeDownRecognizer.numberOfTouchesRequired = 1;
swipeDownRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[imageView addGestureRecognizer:swipeDownRecognizer];
[swipeDownRecognizer release];
//swipe Left
swipeLeftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeLeft:)];
swipeLeftRecognizer.delegate = self;
swipeLeftRecognizer = (UISwipeGestureRecognizer *)swipeLeftRecognizer;
swipeLeftRecognizer.numberOfTouchesRequired = 1;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipeLeftRecognizer];
[swipeLeftRecognizer release];
//swipe Right
swipeRightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeRight:)];
swipeRightRecognizer.delegate = self;
swipeRightRecognizer = (UISwipeGestureRecognizer *)swipeRightRecognizer;
swipeRightRecognizer.numberOfTouchesRequired = 1;
swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeRightRecognizer];
[swipeRightRecognizer release];
}
You have to enable the user interaction on your UIImageView. Use the code below
imageView.userInteractionEnabled = YES;
Related
I am tyring to add UISegmentedControl to UIScrolview programmatically.
I am using below code..
I don't why the scrollview is not enabled.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)];
scroll.contentSize = CGSizeMake(320, 700);
scroll.showsHorizontalScrollIndicator = YES;
NSArray *menu = [NSArray arrayWithObjects:[UIImage imageNamed:#"Accountimg.png"],[UIImage imageNamed:#"Challengesimg.png"],[UIImage imageNamed:#"Profileimg.png"],[UIImage imageNamed:#"Challengesimg.png"],nil];
UISegmentedControl *menuSegmentedControl = [[UISegmentedControl alloc] initWithItems:menu];
menuSegmentedControl.frame = CGRectMake(0,0,450,40);
menuSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[scroll addSubview:menuSegmentedControl];
[menuScrollView addSubview:scroll];
[menuSegmentedControl release];
[scroll release];
I am unable to scroll horizontally..
Thanks in advance
UPDATE:
I am able to get the horizontal scroll.
Code updated:
menuSegmentedControl.frame = CGRectMake(0,0,500,35);
menuSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
menuScrollView.contentSize = CGSizeMake(menuSegmentedControl.frame.size.width, menuSegmentedControl.frame.size.height);
menuScrollView.showsVerticalScrollIndicator = NO;
[menuScrollView addSubview:menuSegmentedControl];
menuScrollView.clipsToBounds = YES;
Thank you.
hi iam trying to put a uisegmented controls over a uiscrollview beacuse i want them to scroll horizontally this is the code i tried
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 600, 300)];
scroll.contentSize = CGSizeMake(500, 50);
scroll.showsHorizontalScrollIndicator = YES;
scroll.pagingEnabled = YES;
scroll.showsHorizontalScrollIndicator = YES;
scroll.autoresizingMask = YES;
scroll.autoresizesSubviews = YES;
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three",#"four",#"Five", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0,10, 500, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 4;
[scroll addSubview:segmentedControl];
[self.view addSubview:scroll];
any one please help me to accomplish this . thanks in advance happy coding
The frame for the scroll View must be less than the Content size of scroll view. If you will increase your content size definitely it will work. For example:-
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
scroll.contentSize = CGSizeMake(500, 50);
scroll.showsHorizontalScrollIndicator = YES;
scroll.pagingEnabled = YES;
scroll.showsHorizontalScrollIndicator = YES;
scroll.autoresizingMask = YES;
scroll.autoresizesSubviews = YES;
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three",#"four",#"Five", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0,10, 500, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 4;
[scroll addSubview:segmentedControl];
[self.view addSubview:scroll];
If its supportive please mark the answer correct. Thanks very much.
I want four ScrollViews like iCarousel's CoverFlow in View programmatically. My Code is
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//set up data
wrap = YES;
self.items = [NSMutableArray array];
for (int i = 0; i < NUMBER_OF_ITEMS; i++)
{
[items addObject:[NSNumber numberWithInt:i]];
}
carousel1 = [[carousel1 alloc] init];
carousel2 = [[carousel2 alloc] init];
carousel3 = [[carousel3 alloc] init];
carousel4 = [[carousel4 alloc] init];
carousel1.frame = CGRectMake(0, 0, 320, 120);
carousel2.frame = CGRectMake(0, 120, 320, 120);
carousel3.frame = CGRectMake(0, 240, 320, 120);
carousel4.frame = CGRectMake(0, 360, 320, 120);
carousel1.delegate = self;
carousel2.delegate = self;
carousel3.delegate = self;
carousel4.delegate = self;
carousel1.dataSource = self;
carousel2.dataSource = self;
carousel3.dataSource = self;
carousel4.dataSource = self;
[self.view addSubview:carousel1];
[self.view addSubview:carousel2];
[self.view addSubview:carousel3];
[self.view addSubview:carousel4];
//configure carousel
carousel1.type = iCarouselTypeCoverFlow2;
carousel2.type = iCarouselTypeCoverFlow2;
carousel3.type = iCarouselTypeCoverFlow2;
carousel4.type = iCarouselTypeCoverFlow2;
}
but fail with four warnings with out placing the carousels view on the View. Please help me to overcome this problem. Thanks in advance.
Shouldn't you be initialising them with [[iCarousel alloc] init] instead of [[carousel1 alloc] init]?
Is there any way to set the view page modal during network connecting? I mean all controllers in page do not response the user operation if the network is connecting?
I updated code like:
- (void)loadView
{
[super loadView];
_hudView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 170, 170)];
_hudView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
_hudView.clipsToBounds = YES;
_hudView.layer.cornerRadius = 10.0;
_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
_activityIndicatorView.frame = CGRectMake(65, 40, _activityIndicatorView.bounds.size.width, _activityIndicatorView.bounds.size.height);
[_hudView addSubview:_activityIndicatorView];
[_activityIndicatorView startAnimating];
_captionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
_captionLabel.backgroundColor = [UIColor clearColor];
_captionLabel.textColor = [UIColor whiteColor];
_captionLabel.adjustsFontSizeToFitWidth = YES;
_captionLabel.textAlignment = UITextAlignmentCenter;
_captionLabel.text = #"Loading...";
[_hudView addSubview:_captionLabel];
NSArray *buttonNames = [NSArray arrayWithObjects:#"hehe",#"xixi",nil];
UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
segmentControl.frame = CGRectMake(0, 0, TTApplicationFrame().size.width/2, TT_ROW_HEIGHT-10);
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentControl.momentary = NO;
segmentControl.tintColor = [UIColor darkGrayColor];
segmentControl.selectedSegmentIndex = 0;
[segmentControl addTarget:self action:#selector(switchPage:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentControl;
TT_RELEASE_SAFELY(segmentControl);
self.view = [[[UIView alloc] initWithFrame:TTApplicationFrame()] autorelease];
self.tableView = [[[UITableView alloc] initWithFrame:TTApplicationFrame() style:UITableViewStylePlain] autorelease];
self.tableView.rowHeight = 80.f;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:self.tableView];
[self reload];
[self updateView];
}
and show it like:
- (void)showLoading:(BOOL)show {
if (show) {
self.loadingView = _hudView;
}
else {
self.loadingView = nil;
}
}
As I said in comment, it still cannot cover the buttons of UISegmentedControl, user still can click those buttons...
Instead of using a modalViewController, create a HUD (one of the nice views with a UIActivityIndicators you'll see in the TTCatalogue app) that will block the view and inform the user something is happening while you're making a request.
Just insert them by a [self.view addSubview:theHUDview];.
It's not necessarily good design though, instead, make your network calls asynchronously, in the background.
i have implemented the following viewDidLoad() Method:
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
AFOpenFlowView *openFlow = self.view;
theControl = [[UIControl alloc] initWithFrame:self.view.frame];
NSString *imageName;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:#selector(testMethode) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"<<" forState:UIControlStateNormal];
[btn setHidden:YES];
[theControl addSubview:btn];
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[openFlow setImage:scaledImage forIndex:i];
// [(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
[aktuellesImage release];
}
[theControl addSubview:openFlow];
[theControl setUserInteractionEnabled:YES];
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
And the following touchesEnded() Method:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[btn setHidden:NO];
}
But the touchesEnded doesn't work, and the Button will not shown when i touch the picture, does anybody know what is the Problem???
Greetings Marco
Make sure your view has user interaction enabled in order to register touches. This can be set in your View Controller thusly:
self.view.userInteractionEnabled = YES;