UIScrollView with UIButtons 'skipping' when finger drags outside of its superviews bounds - objective-c

I have a scrollView set up as the titleView of a navigationBar. The contentView of the scrollView has 4 buttons in it, all laid out dynamically. The buttons need to cancel with drags and scrolling, so I've subclassed them to setEnabled:NO on themselves when touchesMoved.
When I try to scroll the scrollView, as soon as I pass over the bounds of the superview (i.e., passing the bottom or top bounds navigationBar), the scrollview 'skips' 8 or 11 pixels, depending on if its the top or the button. It has something to do with the UIButtons, because when I just use UILabels the problem doesn't persist.
Relevant code:
Scrollview Initialization:
menuWidth = 196;
rowHeight = 40;
rowMargin = self.frame.size.height - rowHeight;
//drop down menu stuff
self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
scrollView.backgroundColor = [UIColor clearColor];
scrollView.contentSize = CGSizeMake(self.frame.size.width, (self.frame.size.height)*choices.count);
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.clipsToBounds = YES;
scrollView.canCancelContentTouches = YES;
scrollView.delaysContentTouches = YES;
scrollView.delegate = self;
[self layoutMenu];
[self addSubview:scrollView];
[scrollView release];
Button initialization (-layoutMenu):
SKScrollableButton *tempButton;
NSString *text;
for (int i = 0; i < choices.count; i++) {
text = (NSString*)[choices objectAtIndex:i];
if (i == 0) {
text = [text stringByAppendingString:downArrow];
}
CGRect labelRect = CGRectMake(0, i*(rowHeight + rowMargin), menuWidth, rowHeight - 7);
tempButton = [[SKScrollableButton alloc] initWithFrame:labelRect];
[tempButton setTitle:text forState:UIControlStateNormal];
[tempButton addTarget:self action:#selector(buttonDown:) forControlEvents:UIControlEventTouchDown];
[tempButton addTarget:self action:#selector(buttonUpInside:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:tempButton];
[tempButton release];
}
No events get called on the buttons when the skipping happens, and only normal-seeming scrollViewDidScroll delegate calls happen on the scrollView.
I've tested and the skipping definitely happens when passing the bounds of the superview, not the bounds of the scrollView itself.

Related

Horizontal UIScrollView with multiple textfields to display

I'm new to Xcode. I want to make a UIScrollView with multiple pages and each page having multiple UITextFields which vary on each page. I made UIScrollView with paging enabled. Now I'm stuck at displaying textfield's on scroll pages.
Here is my code so far:
//set the paging to yes
self.scrollview.pagingEnabled = YES;
// create 5 pages
NSUInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++)
{
//set the origin of the sub view
CGFloat myOrigin = i * self.view.frame.size.width;
//create the sub view and allocate memory
myView = [[UIView alloc] initWithFrame:CGRectMake(myOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//create a label and add to the sub view
CGRect myFrame = CGRectMake(10.0f, 10.0f, 200.0f, 25.0f);
textLabel = [[UILabel alloc] initWithFrame:myFrame];
textLabel.font = [UIFont boldSystemFontOfSize:16.0f];
textLabel.textAlignment = NSTextAlignmentLeft;
[myView addSubview:textLabel];
//create a text field and add to the sub view
myFrame.origin.y += myFrame.size.height + 10.0f;
textField = [[UITextField alloc] initWithFrame:myFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.tag = i+1;
[myView addSubview:textField];
//set the background to different color
//set the scroll view delegate to self so that we can listen for changes
self.scrollview.delegate = self;
//add the subview to the scroll view
[self.scrollview addSubview:myView];
}
//scroll horizontally
self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
self.scrollview.frame.size.height);
//we set the origin to the 1rd page
CGPoint scrollPoint = CGPointMake(self.view.frame.size.width * 0, 0);
//change the scroll view offset the the 1rd page so it will start from there
[scrollview setContentOffset:scrollPoint animated:YES];
[self.view addSubview:self.scrollview];
}
Use This code here textfieldCount is
for (int j=0; j < textFieldCount; j++)
{
myFrame.origin.y += myFrame.size.height + 10.0f*(j+1);
UITextField *textField = [[UITextField alloc] initWithFrame:myFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.tag = j+1;
[myView addSubview:textField];
}
Hope you got your answer.

Create a temporary HUD popup notification in Cocoa App

Im trying to have a window pop up to notify the user of an action, kind of like how the Flutter app shows a play/pause notification when a gesture is received.
This comes up for about two seconds and vanishes. The app itself is run from the menu bar so this pop up doesn't take control from whatever application is currently in focus but rather just shows on top of it and cannot be interacted with. I basically want to recreate exactly this. Say in the body of
- (void)popup:(id)sender {}
This is essentially very similar to a toast on android.
Edit: I looked closer at the flutter app and all it does is display two images (.png) one for the huge pause and another to indicate what application its in (in this case iTunes). Question now is how do I display an image like this.
You should take a look at this:
https://github.com/Foxnolds/MBProgressHUD-OSX
or Growl:
http://growl.info/documentation/developer/
To replicate this I added a 'HUD Window" from the interface builder which is just an NSPanel. I then added a image view to the panel and set the panel's colour to clear. I was then able to position the panel where I wanted and overlay the appropriate image.
I'm using this code:
static void showHud(NSString* text, int width, int height, CGFloat duration, int fontSize) {
NSWindow* window = [NSWindow new];
[window setFrame:NSMakeRect(0,0,width,height) display:NO];
[window center];
window.titleVisibility = NSWindowTitleHidden;
window.styleMask = NSWindowStyleMaskBorderless;
window.alphaValue = 0.9;
window.movableByWindowBackground = YES;
[window setLevel: NSStatusWindowLevel];
[window setBackgroundColor: [NSColor clearColor]];
[window setOpaque:NO];
//[window setHasShadow:NO];
[window makeKeyAndOrderFront:NSApp];
NSVisualEffectView *view = [[NSVisualEffectView new] initWithFrame:window.contentView.bounds];;
view.translatesAutoresizingMaskIntoConstraints = NO;
[view setBlendingMode:NSVisualEffectBlendingModeBehindWindow];
[view setMaterial:NSVisualEffectMaterialDark];
[view setState:NSVisualEffectStateActive];
//[view setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantDark]];
//[view setWantsLayer:NO];
[view setWantsLayer:YES];
view.layer.cornerRadius = 16.;
//view.layer.shouldRasterize = YES;
//view.layer.rasterizationScale = 0.45;
view.layer.shadowOpacity = 0.1; //0.01;
view.layer.edgeAntialiasingMask = kCALayerTopEdge | kCALayerBottomEdge | kCALayerRightEdge | kCALayerLeftEdge;
[window.contentView addSubview:view];
//window.contentView = view;
NSTextField *label = [[NSTextField alloc] initWithFrame:view.bounds];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
label.stringValue = text;
label.alignment = NSTextAlignmentCenter;
label.textColor = [NSColor whiteColor];
label.backgroundColor = [NSColor clearColor];
label.font = [NSFont fontWithName:#"Arial-BoldMT" size:fontSize];
//NSRect titleRect = [label titleRectForBounds:view.bounds];
NSSize cellSize = [label.cell cellSizeForBounds:view.bounds];
NSSize strSize = cellSize; // [label.attributedStringValue size];
NSRect frame = view.frame;
frame.origin.y = frame.size.height / 2 - strSize.height / 2;
frame.size.height = strSize.height;
label.frame = frame;
[view addSubview:label];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.duration = duration;
window.animator.alphaValue = 0.;
} completionHandler:^{
[window close];
}];
}

UIButton touch up inside event only fired if done in title

I am customizing a UITableViewCell with several subviews:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_mainView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
_hiddenOptionsView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
_menuView = [[CellMenuView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 20.0f)];
[self addSubview:_menuView];
[self addSubview:_hiddenOptionsView];
[self addSubview:_mainView];
}
return self;
}
The class CellMenuView is a UIView subclass which has two UIButtons with their counterpart target actions setup on initialization:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIImageView *background = [[UIImageView alloc] initWithFrame:frame];
[background setImage:[UIImage imageNamed:#"cell_menu_bg.png"]];
[self addSubview:background];
CGFloat buttonX = frame.origin.x;
CGFloat buttonY = frame.origin.y + 3.0f;
CGFloat buttonWidth = frame.size.width / 2.0f;
CGFloat buttonHeight = 10.0f;
_editButton = [UIButton buttonWithType:UIButtonTypeCustom];
_editButton.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
_editButton.backgroundColor = [UIColor clearColor];
_editButton.titleLabel.shadowColor = [UIColor blackColor];
_editButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.5f);
_editButton.titleLabel.font = [UIFont boldSystemFontOfSize:22.0f];
[_editButton setTitle:#"Edit" forState:UIControlStateNormal];
[_editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_editButton addTarget:self action:#selector(editButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_editButton];
_fireButton = [UIButton buttonWithType:UIButtonTypeCustom];
_fireButton.frame = CGRectMake(buttonWidth, buttonY, buttonWidth, buttonHeight);
_fireButton.backgroundColor = [UIColor clearColor];
_fireButton.titleLabel.shadowColor = [UIColor blackColor];
_fireButton.titleLabel.shadowOffset = CGSizeMake(0.0f, -1.5f);
_fireButton.titleLabel.font = [UIFont boldSystemFontOfSize:22.0f];
[_fireButton setTitle:#"Fire" forState:UIControlStateNormal];
[_fireButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_fireButton addTarget:self action:#selector(fireButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_fireButton];
return self;
}
}
I have implemented the target action methods but they aren't being executed. Instead, didSelectRowAtIndexPath: takes precedence over the touch up events inside buttons every time I pressed one of them.
The only way I have been able to fire the events on buttons is making sure that one of the letters in UIButton's title is touched (using the simulator, of course).
I must say that _menuView is hidden behind the other subviews and shown below the cell when a custom accessory button in cell is pressed. It appears by modifying the Y origin and disappears by setting it to 0.0f again.
I think it may be related to the view hierarchy, because I haven't had any problems in the past by adding buttons directly to the cell. But I'm just guessing here.
How can I make the events on buttons take precedence over the didSelectRowAtIndexPath: method?
Thanks!
Implement
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
And return nil for the indexPath you don't want selected; this will prevent didSelectRowAtIndexPath from being called on that cell, and (hopefully), your custom action will be called.
Update: After implementing this myself (adding borders to the buttons), this is what I have.
The code to achieve this (which is different than yours), is as follows:
CellMenuView.m
self.backgroundColor = [UIColor blackColor];
_editButton.layer.borderColor = [UIColor whiteColor].CGColor;
_editButton.layer.borderWidth = 2;
_fireButton.layer.borderColor = [UIColor whiteColor].CGColor;
_fireButton.layer.borderWidth = 2;
UITableViewCell subclass
// Change in height to make it taller
_menuView = [[TestBtn alloc] initWithFrame:CGRectMake(0.0f, 20.0f, 320.0f, 60.0f)];
UITableViewController subclass
self.tableView.rowHeight = 100;
After testing, mine works as expected. If I click within the white squares, the button action is performed as normal, and didSelectRowAtIndexPath is not called. didSelectRowAtIndexPath is only called when I click outside of the buttons.
I think the issue here might be your button heights/frames, as well as your row height. Add borders to your buttons, to check its clickable area, and increase the height of the button to increase the clickable area.

Hijack "swipe to next page" in UIScrollView

i'd like to prevent scrolling on the 3rd page of my UIScrollview and "hijack the swipe" gesture to trigger sth. else. Afer this action I'd like to reactive scrolling.
This does not work.
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if(scrollView.contentOffset.x == self.view.frame.size.width * 2 ) {
// disable scrolling
scrollView.scrollEnabled = NO;
}
}
// hijack the next scrolling event
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
This delegate is not called when scrollEnabled = NO
Thanks for helping
EDIT EventHandler ist not called ;-(
- (void)viewDidLoad
{
[super viewDidLoad];
// Default background color
self.view.backgroundColor = [UIColor redColor];
// Create scroll view
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[scrollView addGestureRecognizer:recognizer];
[recognizer release];
[scrollView delaysContentTouches];
// Create subviews (pages)
NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++) {
// x pos
CGFloat yOrigin = i * self.view.frame.size.width;
// Create subview and add to scrollView
UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
pageView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scrollView addSubview:pageView];
[pageView release];
}
// Set contentsize
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
// Add scrollView to view and release
[self.view addSubview:scrollView];
[scrollView release];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"swipe!!!!");
scrollView.scrollEnabled = YES;
}
If you disable the scroll view:
scrollView.scrollEnabled = NO;
it is unavoidable that the delegate method is not called, so you need an alternative way to handle the swipe while in hijack mode. One thing you could try is using an UISwipeGestureRecognizer: instead of simply disabling scrolling, you could associate a UISwipeGestureRecognizer to you view and handle the swipe from the handler method:
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:recognizer];
and in handleSwipeFrom you would reenable scrolling:
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
// do your hijack here
scrollView.scrollEnabled = YES;
}

Extending UIScrollView when it fills up with more content

Ok, so i got a scrollview to load when an user logs in to my application. When the user is authenticated, i need to create a scrollview programmatically. To do so, i have the following code:
[scrollView removeFromSuperview];
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, self.view.frame.size.height)];
view.backgroundColor = [UIColor greenColor];
//-- get the y-coordinate of the view
CGFloat viewCenterY = self.view.center.y;
//--calculate how much visible space is left
CGFloat freeSpaceHeight = applicationFrame.size.height - keyboardBounds.size.height;
//-- calculate how much the scrollview must scroll
CGFloat scrollAmount = viewCenterY - freeSpaceHeight / 2.0;
if(scrollAmount < 0) scrollAmount = 0;
//set the new scrollsize contentview
view.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);
//scroll the scrollview
[view setContentOffset:CGPointMake(0, scrollAmount) animated:YES];
for(int v = 0; v < 15; v++){
CGFloat yCoord = v * 100;
UILabel *mijnLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, yCoord, 200, 50)];
mijnLabel.text = #"1337";
mijnLabel.font = [UIFont fontWithName:#"Verdana" size:20];
mijnLabel.textColor = [UIColor redColor];
[view addSubview:mijnLabel];
[mijnLabel release];
}
[self.view addSubview:view];
[view release];
}
How do i extend my view according to the amount of labels i added? I also want to know how i can make my scrollview fill up the entire screen.. My scrollview is now only filling up the screen when in portrait mode.
This is the login screen that gets removed:
[scrollView removeFromSuperview];
you need to keep track of numbers of label that you are adding into the scroll view. suppose if your label's height is 50 then take a variable and make a total of all label's height and give a new frame to your scroll view. like :
view.frame = CGRectmake(0,0,applicationFrame.size.width,totalHeightOfLabel);
[self.view addSubview:view];
[view release];
hope this will give a idea...