Question: Currently I'm struggling with receiving touch on a UISlider which is layed out via a xib layouts + a UICollectionView.

My layout which does contain the UISlider does look like this:

[![the UISlider layout](https://i.stack.imgur.com/xb7Uf.png)](https://i.stack.imgur.com/xb7Uf.png)

The UICollectionView itself works perfectly fine. The layout with the UISlider is layed out with several other views.

Currently I experience the UISlider not to react to any user interaction at all. I can scroll inside the collectionView, but when I try to change the value of the slider it just stays as it is.

I found some solutions on SO, like:

[Pass UICollectionView touch event to its parent UITableViewCell](https://stackoverflow.com/questions/33512344/pass-uicollectionview-touch-event-to-its-parent-uitableviewcell?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa)

[How do you stop UITapGestureRecognizer from catching EVERY tap?](https://stackoverflow.com/questions/4885693/how-do-you-stop-uitapgesturerecognizer-from-catching-every-tap) 

but the solutions seem not to be really complete or helpful to me, since I'm quite new to ios and the touch handling.

I'd appreciate any help!

Answer: 
You could use `rowsum()` with a little work. This is a loop-less solution that will be very efficient.

```
l <- lengths(W)
rowsum(a[unlist(W)], rep(1:length(W), l)) / l
#        [,1]
# 1 0.3666667
# 2 0.8000000
# 3 0.4000000

```

The only reason I assign `l` here is to avoid the second calculation of `lengths(W)`. But `lengths()` is also very fast, so you could also do a one-liner as 

```
rowsum(a[unlist(W)], rep(1:length(W), lengths(W))) / lengths(W)

```
This is similar to your for loop but use `sapply` to achieve the task.

```
sapply(1:3, function(i) mean(a[W[[i]]]))
# [1] 0.3666667 0.8000000 0.4000000

```

We can also use `tapply`.

```
# Create b based on the index in the list W
index <- unlist(W)
b <- a[index]

# Calcualte the mean for every three numbers
tapply(b, rep(1:3, each = 3), mean)

#         1         2         3 
# 0.3666667 0.8000000 0.4000000 

```