Scroll Wheel event capture in a Catalyst-converted Mac app - macos

When working with a Mac app which was converted from iOS using Catalyst, the usual ways to capture a mouse's scroll wheel activity for Mac such as with
(void)scrollWheel:(NSEvent *)event;
do not work as NSEvent apparently is not supported when building a Catalyst converted app.
The object I need to control is in a regular image container and not in a scroll view container. I am simply trying to use the scroll wheel to change the loaded image. Trackpad activity works fine but capturing the scroll wheel so far has been elusive.
Thanks!

Use a UIPanGestureRecognizer with allowedScrollTypesMask set to UIScrollTypeMaskDiscrete:
// pan gesture to recognize mouse-wheel scrolling (zoom)
UIPanGestureRecognizer * scrollWheelGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleScrollWheelGesture:)];
scrollWheelGesture.allowedScrollTypesMask = UIScrollTypeMaskDiscrete; // only accept scroll-wheel, not track-pad
scrollWheelGesture.maximumNumberOfTouches = 0;
[self.view addGestureRecognizer:scrollWheelGesture];
and then
- (void)handleScrollWheelGesture:(UIPanGestureRecognizer *)pan
{
CGPoint delta = [pan translationInView:self.view];
CGFloat zoom = (1000 + delta.y) / 1000;
[self adjustZoomBy:zoom];
}

Swift 5:
func setupScrollWheel() {
let scrollWheelGesture = UIPanGestureRecognizer(target: self, action: #selector(scrollWheelGestureRecognizer(_:)))
scrollWheelGesture.allowedScrollTypesMask = .discrete
scrollWheelGesture.maximumNumberOfTouches = 0
view.addGestureRecognizer(scrollWheelGesture)
}
#objc func scrollWheelGestureRecognizer(_ recognizer: UIPanGestureRecognizer) {
let delta = recognizer.translation(in: view)
}

Related

xcode UITapGestureRecognizer on scrollview not calling until second tap

I have the following code to dismiss the keyboard if the user taps the background. It works fine if the scrollview is in the PointZero position, but if the user scrolls the view and then selects the textview, it doesn't call the "dismissKeyboard' method until the 2nd background tap.
On the first tap (for some reason) moves the scrollview offset to align with the scrollview frame to the screen bottom. The second tap will dismiss the keyboard and run the code below. I know it has to do with the scrollview. Any help would be appreciated.
Thanks
- (void)viewDidLoad {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[_scrollView addGestureRecognizer:tapGesture];
}
-(void)dismissKeyboard {
[self.view endEditing:YES];
}
- (void)keyboardWasShown:(NSNotification *)notification {
scrollViewRect = _scrollView.contentOffset.y;
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
keyboardSize.height += 10;
CGFloat viewBottom = CGRectGetMaxY(self.scrollView.frame);
if ([_itemNotes isFirstResponder]) {
CGFloat notesBottom = CGRectGetMaxY(_itemNotes.frame);
viewBottom -= notesBottom;
if (viewBottom < keyboardSize.height) {
keyboardSize.height -= viewBottom;
CGPoint scrollPoint = CGPointMake(0.0, keyboardSize.height);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
else {
[self.scrollView setContentOffset:CGPointZero animated:YES];
}
}
else {
[self.scrollView setContentOffset:CGPointZero animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification *)notification {
CGPoint scrollPoint = CGPointMake(0.0, scrollViewRect);
[self.scrollView setContentOffset:scrollPoint animated:YES];
}
EDIT:
So I figured out a solution but it seems like there must be a better way to handle this. The problem was because I was setting the contentOffset of the scrollView so that the contentSize was beyond the screen boundaries. Thus the first tap was moving the scrollView contentOffset back within the screen boundaries and the second was performing the tap gesture. I will post my solution below hoping that someone has a better answer.
I would recommend setting
_scrollView.layer.borderColor = [UIColor redColor].CGColor;
_scrollView.layer.borderWidth = 1;
This will show you exactly where your scrollview boundaries are, which may not be where you think they are, or may be covered by something else. Also, when I open the keyboard, I generally set the scrollview frame bottom to the top of the keyboard. Otherwise, you may have content below the keyboard you can't get to. Not sure if this is exactly related to your issues.
I am assuming there must be a better solution to this but I was able to solve the issue by extending the contentSize when the keyboard is displayed and then shrinking it back down when the keyboard is hidden.
Set a float (scrollViewHeight) to hold the original content size for the reset.
//add this right before setting the content offset
scrollViewHeight = _scrollView.contentSize.height;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width , scrollViewHeight + keyboardSize.height);
//add this right before reseting the content offset
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width , scrollViewHeight);
It really seems like there must be a better way that I'm not aware of. I will have to go back through the documentation to see if there is another way.

QuickType Bar on the Keyboard

As you all might be knowing about the new Quick Type bar on the keyboard.
In my application , I have put a custom TextView bar on the keyboard. But because of QuickType Bar , my textview gets hidden.
I want to know , Is there any property or method to know whether the QuickType Bar is open or not ?
There is nothing which can tell you whether the QuickType bar is active or not but you can register for the UIKeyboardWillChangeFrameNotification notification with this code and with that you can get info about the height of the keyboard.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillChangeFrame:) name:UIKeyboardDidChangeFrameNotification object:nil];
Use the UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey values in the passed userInfo dictionary to retrieve the current and future frames of the keyboard. You can use the following code as a reference.
- (void)keyboardWillChangeFrame:(NSNotification*)notification
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
// Manage your other frame changes
}
Hope this helps. It will get called every time the keyboard changes its frame.
As the other answers have suggested, the UIKeyboardWillChangeFrameNotification will fire every time the keyboard gets a new frame. This includes when the keyboard will show and hide, and when the QuickType bar will show and hide.
The thing is, this is exactly the same notification as the UIKeyboardWillShowNotification, just with a different name. Therefore, if you're already implementing a method for the UIKeyboardWillShowNotification, you're good to go.
With one exception, though. When you get the keyboard frame in your method for handling the UIKeyboardWillShowNotification, you must make sure you access it via the UIKeyboardFrameEndUserInfoKey, not the UIKeyboardFrameBeginUserInfoKey. Otherwise, it will get the correct frame when the keyboard is shown/hidden, but not when the QuickType bar is.
So, the code in your method for handling the UIKeyboardWillShowNotification should look something like this (in Swift):
func keyboardWillShow(notification: NSNotification) {
let info = notification.userInfo!
keyboardRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
keyboardRect = yourView.convertRect(keyboardRect, fromView: nil)
// Handling the keyboard rect by changing frames, content offsets, constraints, or whatever
}
- (void)keyboardFrameChanged:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGPoint from = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].origin;
CGPoint to = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin;
float height = 0.0f;
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
height = to.x - from.x;
} else {
height = to.y - from.y;
}
[self setContentSize:CGSizeMake(self.frame.size.width, self.frame.size.height + height)];
}

iCarousels Getting Stuck in iOS 7

I have implemented the iCarousels in my project. But after updating the app for iOS 7, my iCarousels are getting stuck in between. Its working fine in iOS 6 and 5. the problem in iOS 7 is that my scroll view below the iCarousel view is getting called first sometimes when I touch my carousel view. Can anyone help me out here?
Is the solution in the following method's return value:
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
I tried many things here, and it works fine for few times, and again after some time it start getting stuck because the scroll view takes the touch from its subview(iCarousel view) and call its own delegate methods before the iCarousel's delegate method.
I am not using any gesture recognizer. I am using scroll view because i have iCarousel view and another view resting on UIScrollView, so that I can use pull to refresh as well.
The following delegate methods I am using, and changing in carouselItemWidth has decreased the stuck problem, but it still persists
- (CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
CGFloat tilt = 0.65f;
CGFloat spacing = 0.28f; // should be ~ 1/scrollSpeed;
CGFloat clampedOffset = fmaxf(-1.0f, fminf(1.5f, offset));
CGFloat itemWidth = 320;
CGFloat x = (clampedOffset * 0.5f * tilt + offset * spacing) * itemWidth;
CGFloat z = fabsf(clampedOffset) * -itemWidth * 0.5f;
transform = CATransform3DTranslate(transform, 0.0f, x, z);
transform = CATransform3DRotate(transform, -clampedOffset * M_PI_2 * tilt, -1.0f, 0.0f, 0.0f);
//DLog(#"offset: %f, %#", offset, [NSValue valueWithCATransform3D:transform]);
return transform;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
//note: placeholder views are only displayed on some carousels if wrapping is disabled
return 0;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
//usually this should be slightly wider than the item views
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
return 270;
}
else
{
return 250;
}
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
return NO;
}
The problem seems to be that when a scrollview receives a touch, it waits a second to see if it should handle it before passing it to the carousel.
You can (mostly) fix this by setting scrollView.delaysContentTouches = NO;
It's still a bit clunky if you try to swipe the carousel when the scrollview is moving/decelerating however. You will have to wait until it stops moving to interact with the carousel.
I'm investigating if there's a better way to fix this.
UPDATE:
I still don't have a proper general-purpose fix for this yet, but as a workaround, you can add this method to your local copy of iCarousel:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return [gesture isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
This forces iCarousel's pan gesture recogniser to take precedence over the one in the scrollView. If you combine this with the delaysContentTouches fix above, you shouldn't have any issues scrolling the carousel when it's inside a tableview or scrollview.

Soft scroll animation NSScrollView scrollToPoint:

I want to create soft animation between transitions in simply UI:
view that moved
When a call scrollToPoint: for move view to point that transition doesn't animate.
I'm newbe in Cocoa programming (iOS is my background). And I don't know how right use .animator or NSAnimationContext.
Also I was read Core Animation guide but didn't find the solution.
The source can be reach on Git Hub repository
Please help !!!
scrollToPoint is not animatable. Only animatable properties like bounds and position in NSAnimatablePropertyContainer are animated. You don't need to do anything with CALayer: remove the wantsLayer and CALayer stuff. Then with following code it is animated.
- (void)scrollToXPosition:(float)xCoord {
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:5.0];
NSClipView* clipView = [_scrollView contentView];
NSPoint newOrigin = [clipView bounds].origin;
newOrigin.x = xCoord;
[[clipView animator] setBoundsOrigin:newOrigin];
[_scrollView reflectScrolledClipView: [_scrollView contentView]]; // may not bee necessary
[NSAnimationContext endGrouping];
}
Swift 4 code of this answer
func scroll(toPoint: NSPoint, animationDuration: Double) {
NSAnimationContext.beginGrouping()
NSAnimationContext.current.duration = animationDuration
let clipView = scrollView.contentView
clipView.animator().setBoundsOrigin(toPoint)
scrollView.reflectScrolledClipView(scrollView.contentView)
NSAnimationContext.endGrouping()
}
The proposed answers have a significant downside: If the user tries to scroll during an ongoing animation, the input will be cause jittering as the animation will forcefully keep on going until completion. If you set a really long animation duration, the issue becomes apparent. Here is my use case, animating a scroll view to snap to a section title (while trying to scroll up at the same time):
I propose the following subclass:
public class AnimatingScrollView: NSScrollView {
// This will override and cancel any running scroll animations
override public func scroll(_ clipView: NSClipView, to point: NSPoint) {
CATransaction.begin()
CATransaction.setDisableActions(true)
contentView.setBoundsOrigin(point)
CATransaction.commit()
super.scroll(clipView, to: point)
}
public func scroll(toPoint: NSPoint, animationDuration: Double) {
NSAnimationContext.beginGrouping()
NSAnimationContext.current.duration = animationDuration
contentView.animator().setBoundsOrigin(toPoint)
reflectScrolledClipView(contentView)
NSAnimationContext.endGrouping()
}
}
By overriding the normal scroll(_ clipView: NSClipView, to point: NSPoint) (invoked when the user scrolls) and manually performing the a scroll inside a CATransaction with setDisableActions, we cancel the current animation. However, we don't call reflectScrolledClipView, instead we call super.scroll(clipView, to: point), which will perform other necessary internal procedures and then perform reflectScrolledClipView.
Above class produces better results:
Here is a Swift 4 extension version of Andrew's answer
extension NSScrollView {
func scroll(to point: NSPoint, animationDuration: Double) {
NSAnimationContext.beginGrouping()
NSAnimationContext.current.duration = animationDuration
contentView.animator().setBoundsOrigin(point)
reflectScrolledClipView(contentView)
NSAnimationContext.endGrouping()
}
}
I know, it's a little bit off topic, but I wanted to have a similar method to scroll to a rectangle with animation like in UIView's scrollRectToVisible(_ rect: CGRect, animated: Bool) for my NSView. I was happy to find this post, but apparently the accepted answer doesn't always work correctly. It turns out that there is a problem with bounds.origin of the clipview. If the view is getting resized (e.g. by resizing the surrounding window) bounds.origin is somehow shifted against the true origin of the visible rectangle in y-direction. I could not figure out why and by how much. Well, there is also this statement in the Apple docs not to manipulate the clipview directly since its main purpose is to function internally as a scrolling machine for views.
But I do know the true origin of the visible area. It’s part of the clipview’s documentVisibleRect. So I take that origin for the calculation of the scrolled origin of the visibleRect and shift the bounds.origin of the clipview by the same amount, and voilà: that works even if the view is getting resized.
Here is my implementation of the new method of my NSView:
func scroll(toRect rect: CGRect, animationDuration duration: Double) {
if let scrollView = enclosingScrollView { // we do have a scroll view
let clipView = scrollView.contentView // and thats its clip view
var newOrigin = clipView.documentVisibleRect.origin // make a copy of the current origin
if newOrigin.x > rect.origin.x { // we are too far to the right
newOrigin.x = rect.origin.x // correct that
}
if rect.origin.x > newOrigin.x + clipView.documentVisibleRect.width - rect.width { // we are too far to the left
newOrigin.x = rect.origin.x - clipView.documentVisibleRect.width + rect.width // correct that
}
if newOrigin.y > rect.origin.y { // we are too low
newOrigin.y = rect.origin.y // correct that
}
if rect.origin.y > newOrigin.y + clipView.documentVisibleRect.height - rect.height { // we are too high
newOrigin.y = rect.origin.y - clipView.documentVisibleRect.height + rect.height // correct that
}
newOrigin.x += clipView.bounds.origin.x - clipView.documentVisibleRect.origin.x // match the new origin to bounds.origin
newOrigin.y += clipView.bounds.origin.y - clipView.documentVisibleRect.origin.y
NSAnimationContext.beginGrouping() // create the animation
NSAnimationContext.current.duration = duration // set its duration
clipView.animator().setBoundsOrigin(newOrigin) // set the new origin with animation
scrollView.reflectScrolledClipView(clipView) // and inform the scroll view about that
NSAnimationContext.endGrouping() // finaly do the animation
}
}
Please note, that I use flipped coordinates in my NSView to make it match the iOS behaviour.
BTW: the animation duration in the iOS version scrollRectToVisible is 0.3 seconds.
https://www.fpposchmann.de/animate-nsviews-scrolltovisible/

PinthToZoom and Pan of UIView with multiple images

I'm developing an iphone application with an UIView that have an image as a background and multiple buttons with image as background. The image is an image of a country and the buttons are the regions/states of that country.
The problem is that the screen is too much little to display all this data, so I want to enable pinch-to-zoom of the UIView which allows user to have a better view of the country.
Inside 'viewDiDLoad' of my UIViewController, I added:
UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:#selector(twoFingerPinch:)]
autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];
and I added twoFingerPinch method:
- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer {
if (recognizer.scale > minValue){
CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
self.view.transform = transform;
}
}
and it work correctly, the problem is that the UIView becomes bigger but user can't navigate inside.
So I switched my UIView to a UIScrollView and inside 'twoFingerPinch' method I added:
if (recognizer.state == UIGestureRecognizerStateEnded){
float scaleForView = mLastScale;
UIScrollView *tempScrollView=(UIScrollView *)self.view;
int newWidth = self.view.frame.size.width * scaleForView;
int newHeight = self.view.frame.size.height * scaleForView;
tempScrollView.contentSize=CGSizeMake(newWidth,newHeight);
mLastScale = 1.0;
}
but it doesn't work well: the UISrollView becomes much bigger but it's not bigger as the image scaled and also is not centered where pinchToZoom started.
How can i solve this problem?
Thank You

Resources